Repository navigation
Expand file tree
/
Copy pathTrappingWater.java
More file actions
79 lines (73 loc) · 2.43 KB
/
Copy pathTrappingWater.java
File metadata and controls
79 lines (73 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.interview.array;
/**
* https://oj.leetcode.com/problems/trapping-rain-water/
*/
public class TrappingWater {
public int trapWater(int input[]){
if(input.length < 2){
return 0;
}
int maxFoundSoFar = 0;
int result = 0;
for(int i = 1; i < input.length; i++){
if(input[i] < input[maxFoundSoFar]){
continue;
}
else{
int minOfTwo = Math.min(input[maxFoundSoFar], input[i]);
for(int j = i-1; j > maxFoundSoFar; j--){
if(minOfTwo > input[j]){
result += minOfTwo - input[j];
}else{
minOfTwo = input[j];
}
}
maxFoundSoFar = i;
}
}
int minOfTwo = input[input.length-1];
for(int j = input.length-2; j > maxFoundSoFar; j--){
if(minOfTwo > input[j]){
result += minOfTwo - input[j];
}else{
minOfTwo = input[j];
}
}
return result;
}
/**
* Keep two array left and right which has max till that point
* either from left side or right side.
* Now for every point i check if it is less than left[i] and right[i].
* If yes then add this to total since water can be trapped here.
* @param input
* @return
*/
public int trapWater1(int input[]){
assert input != null && input.length > 0;
int left[] = new int[input.length];
int right[] = new int[input.length];
left[0] = input[0];
for(int i=1; i < input.length; i++){
left[i] = (input[i] > left[i-1] ? input[i] : left[i-1]);
}
right[input.length-1] = input[input.length-1];
for(int i=input.length-2; i >= 0; i--){
right[i] = (input[i] > right[i+1] ? input[i] : right[i+1]);
}
int result = 0;
for(int i=1; i < input.length-1; i++){
int min = Math.min(left[i], right[i]) - input[i];
if(min > 0){
result += min;
}
}
return result;
}
public static void main(String args[]){
int input[] = {0,1,0,2,1,0,1,3,2,1,2,1};
TrappingWater tw = new TrappingWater();
System.out.println(tw.trapWater(input));
System.out.println(tw.trapWater1(input));
}
}