forked from Ayu-hack/Hacktoberfest2024-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trappingrainwater.cpp
55 lines (44 loc) · 1.16 KB
/
trappingrainwater.cpp
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
#include<bits/stdc++.h>
using namespace std;
int trap(vector<int>& height) {
int n = height.size();
int left = 0, right = n - 1;
int res = 0;
int maxLeft = 0, maxRight = 0;
while (left <= right) {
if (height[left] <= height[right]) {
if (height[left] >= maxLeft) {
maxLeft = height[left];
} else {
res += maxLeft - height[left];
}
left++;
} else {
if (height[right] >= maxRight) {
maxRight = height[right];
} else {
res += maxRight - height[right];
}
right--;
}
}
return res;
}
int main() {
vector<int> arr;
int n;
cout << "Enter the number of elements: ";
cin >> n;
cout << "Enter the height of each bar: ";
for (int i = 0; i < n; i++) {
int height;
cin >> height;
arr.push_back(height);
}
cout << "The water that can be trapped is " << trap(arr) << endl;
return 0;
}
/*
Time Complexity: O(N) because we are using 2 pointer approach.
Space Complexity: O(1) because we are not using anything extra.
*/