Skip to content

Commit

Permalink
Add Trapping Rain Water algorithm cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
riyastar5678 committed Oct 25, 2024
1 parent 24c8ed1 commit 65d0934
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions C++/Tapping_Rainwater.cpp
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 added C++/Tapping_Rainwater.exe
Binary file not shown.

0 comments on commit 65d0934

Please sign in to comment.