-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Trapping Rain Water algorithm cpp
- Loading branch information
1 parent
24c8ed1
commit 65d0934
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int trap(vector<int>& height) { | ||
int n = height.size(); | ||
if (n < 3) return 0; // Not enough bars to trap any water | ||
|
||
// Arrays to store the maximum height to the left and right of each bar | ||
vector<int> leftMax(n), rightMax(n); | ||
|
||
// Fill leftMax array | ||
leftMax[0] = height[0]; | ||
for (int i = 1; i < n; i++) { | ||
leftMax[i] = max(leftMax[i - 1], height[i]); | ||
} | ||
|
||
// Fill rightMax array | ||
rightMax[n - 1] = height[n - 1]; | ||
for (int i = n - 2; i >= 0; i--) { | ||
rightMax[i] = max(rightMax[i + 1], height[i]); | ||
} | ||
|
||
// Calculate the trapped water | ||
int water = 0; | ||
for (int i = 1; i < n - 1; i++) { | ||
// The water trapped on the current bar is determined by the | ||
// minimum of the maximum heights on both sides, minus the current height | ||
int minHeight = min(leftMax[i], rightMax[i]); | ||
if (minHeight > height[i]) { | ||
water += minHeight - height[i]; | ||
} | ||
} | ||
|
||
return water; | ||
} | ||
}; | ||
|
||
int main() { | ||
Solution solution; | ||
vector<int> height = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; | ||
int result = solution.trap(height); | ||
cout << "Total water trapped: " << result << " units" << endl; | ||
|
||
return 0; | ||
} |
Binary file not shown.