-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
42. Trapping Rain Water
63 lines (50 loc) · 1.44 KB
/
42. Trapping Rain Water
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
//Here is the two pointer approach
//by taking auxaliary space of O(n)
//Here I'm giving Both C++ & java Solution
// C++ Solution //and for java solution scroll down
class Solution {
public:
int trap(vector<int>&height) {
//Space COmplexity O(n)
//Time Complexity O(n)
int n = height.size();
vector<int>left(n);
left[0] = height[0];
for(int i=1;i<n;i++){
left[i] = max(left[i-1],height[i]);
}
vector<int>right(n);
right[n-1] = height[n-1];
for(int i=n-2;i>=0;i--){
right[i] = max(right[i+1],height[i]);
}
int ans =0;
for(int i=0;i<n;i++){
ans += (min(right[i],left[i])-height[i]);
}
return ans;
}
};
// Java Solution
class Solution {
public int trap(int[] height) {
//SPace Complexitiy O(n)
int n = height.length;
int left[] = new int[n];
int right[] = new int[n];
left[0] = height[0];
for(int i=1;i<n;i++){
left[i] = Math.max(left[i-1],height[i]);
}
right[n-1] = height[n-1];
for(int i=n-2;i>=0;i--){
right[i] = Math.max(right[i+1],height[i]);
}
int ans=0;
for(int i=0;i<n;i++){
//formula
ans += (Math.min(left[i],right[i])-height[i]);
}
return ans;
}
};