MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / trap

Method trap

TrappingRainWater.java:2–23  ·  view source on GitHub ↗
(int[] height)

Source from the content-addressed store, hash-verified

1class Solution {
2 public int trap(int[] height) {
3 int result=0;
4 int i = 0, j = height.length - 1;
5 int left_max = 0, right_max = 0;
6
7 while(i<=j)
8 {
9 if(height[i]<height[j])
10 {
11 if(height[i]>left_max) left_max=height[i];
12 else result+=left_max-height[i];
13 i++;
14 }
15 else
16 {
17 if(height[j]>right_max) right_max=height[j];
18 else result+=right_max-height[j];
19 j--;
20 }
21 }
22 return result;
23 }
24}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected