Skip to content

Commit

Permalink
Merge pull request #66 from geeky-hypertext629/main
Browse files Browse the repository at this point in the history
Added Leetcode C++ Trapping Rain Water Problem #66
  • Loading branch information
Google-DSC-TMSL authored Oct 12, 2023
2 parents 4fe87cb + 3066465 commit c2afc88
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
| Serialize and Deserialize BST | [Leetcode](https://leetcode.com/problems/serialize-and-deserialize-bst/) | [YouTube](https://www.youtube.com/watch?v=-YbXySKJsX8&t=613s)|[C++](https://github.com/ShreemoyeeMukherjee/ProjectAlgorithms/blob/ShreemoyeeMukherjee-patch-2/Serialize%20and%20Deserialize%20BST) |
| Construct Binary Search Tree from Preorder Traversal | [LeetCode](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/) | [YouTube](https://www.youtube.com/watch?v=UmJT3j26t1I&t=692s) | [C++](https://github.com/ShreemoyeeMukherjee/ProjectAlgorithms/blob/ShreemoyeeMukherjee-patch-2/codes/cpp/Construct%20Binary%20Search%20Tree%20from%20Preorder%20Traversal) |
| First Unique Character in a String | [LeetCode](https://leetcode.com/problems/first-unique-character-in-a-string/) | [YouTube](https://www.youtube.com/watch?v=St47WCbQa9M) | [Java](./codes/java/FirstUniqueCharacterinaString.java) |
| Trapping Rain Water | [LeetCode](https://leetcode.com/problems/trapping-rain-water/description/) | [YouTube](https://www.youtube.com/watch?v=m18Hntz4go8) | [C++](./codes/cpp/TrappingRainWater.cpp) |
| Interleaving Strings | [LeetCode](https://leetcode.com/problems/interleaving-string/) | [YouTube](https://www.youtube.com/watch?v=3Rw3p9LrgvE) | [Python](./codes/python/InterleavingStringsProg.py) |
| Group Anagrams | [LeetCode](https://leetcode.com/problems/group-anagrams/) | [YouTube](https://www.youtube.com/watch?v=ptgykfAEax8) | [Java](./codes/java/GroupAnagramsProg.java) |
| Binary Tree Right Side View | [LeetCode](https://leetcode.com/problems/binary-tree-right-side-view/) | [YouTube](https://www.youtube.com/watch?v=jCqIr_tBLKs&t=390s) | [C++](./codes/cpp/BinaryTreeRightSideViewProg.cpp) |
Expand Down
25 changes: 25 additions & 0 deletions codes/cpp/TrappingRainWater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<bits/stdc++.h>
class Solution {
public:
int trap(vector<int>& height) {
vector<int> lmax(height.size(),0);
vector<int> rmax(height.size(),0);
int i;
lmax[0]=height[0];
for(i=1;i<height.size();i++)
{
lmax[i]=max(lmax[i-1],height[i]);
}
rmax[height.size()-1]=height[height.size()-1];
for(i=height.size()-2;i>=0;i--)
{
rmax[i]=max(rmax[i+1],height[i]);
}
int res=0;
for(i=0;i<height.size();i++)
{
res+=min(lmax[i],rmax[i])-height[i];
}
return res;
}
};

0 comments on commit c2afc88

Please sign in to comment.