Leetcode 42 Trapping Rain Water Solution in java | Hindi Coding Community

0

 



Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.




public class Solution {
public int trap(int[] height) {
int pre, rs = 0, Pos = 0, length = height.length, index;
for(index = 1; index<length; index++) {
if(height[index]>=height[Pos]) Pos = index;
}
for(pre = index = 0; index<Pos; index++) {
if(height[index]<pre) rs += (pre-height[index]);
else pre = height[index];
}
for(pre = 0, index = length-1; index>Pos; index--) {
if(height[index]<pre) rs += (pre-height[index]);
else pre = height[index];
}
return rs;
}
}

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !