Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Trapping Rain Water problem #73

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
| Rotate Image | [LeetCode](https://leetcode.com/problems/rotate-image/) | [YouTube](https://www.youtube.com/watch?v=Y72QeX0Efxw) | [Java](./codes/java/RotateImage.java) |
| Substring with concatenation of all words | [LeetCode](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [YouTube](https://www.youtube.com/watch?v=_MGzsJ0F8lE) | [Java](./codes/java/SubstringWithConcatenationOfAllWords.java) |
|Largest rectangular sub-matrix having sum divisible by k| [GFG](https://www.geeksforgeeks.org/largest-rectangular-sub-matrix-sum-divisible-k/) | [YouTube](https://www.youtube.com/watch?v=_MGzsJ0F8lE) | [Python](./codes/python/largest_rectangular_sub_matrix_sum_divisbl_by_k)
|Sum of Subarray Minimums |[LeetCode](https://leetcode.com/problems/sum-of-subarray-minimums/) |[Youtube](https://youtu.be/9-TXIVEXX2w?si=ktLhMFP8uMgkjXp5) |[C++](./codes/cpp/sum_of_subarray_minimums.cpp)
|Trapping Rain Water |[LeetCode](https://leetcode.com/problems/trapping-rain-water/) |[Youtube](https://youtu.be/m18Hntz4go8?si=tIof7wcU0hnYjbyk) |[C++](./codes/cpp/trapping_rainwater.cpp)
|Next Greater Element III |[LeetCode](https://leetcode.com/problems/next-greater-element-iii/) |[Youtube](https://youtu.be/fOeD3CW2c7c?si=n5lOpXS44P7R6q-y)|[C++](./codes/cpp/next_greater_element3.cpp)
| Largest rectangular sub-matrix having sum divisible by k| [GFG](https://www.geeksforgeeks.org/largest-rectangular-sub-matrix-sum-divisible-k/) | [YouTube](https://www.youtube.com/watch?v=_MGzsJ0F8lE) | [Python](./codes/python/largest_rectangular_sub_matrix_sum_divisbl_by_k)
| Word Break |[LeetCode](https://leetcode.com/problems/word-break/) |[Youtube](https://youtu.be/Sx9NNgInc3A?si=tzljO1p5FRdEaAPf) |[C++](./codes/cpp/word_break.cpp)
Expand Down
31 changes: 31 additions & 0 deletions codes/cpp/sum_of_subarray_minimums.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int sumSubarrayMins(vector<int>& arr) {
int n = arr.size();
int mod = 1e9 + 7;
stack<int> st;
vector<int> dp(n, 0);
int sum = 0;
for(int i =0 ;i < n ; i ++ ){
while(!st.empty() && arr[st.top()] > arr[i]){
st.pop();
}
if(st.empty()){
dp[i] = (i+1)*arr[i];
}else{
dp[i] = dp[st.top()] + (i - st.top())*arr[i];
}
sum = (sum + dp[i])%mod;
st.push(i);
}
return sum;
}
};
int main(){
vector<int> v = {3,1,2,4};
Solution ob;
cout << (ob.sumSubarrayMins(v));
return 0;
}
43 changes: 43 additions & 0 deletions codes/cpp/trapping_rainwater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// C++ implementation of the approach

#include <bits/stdc++.h>
#include<vector>
using namespace std;

// Function to return the maximum
// water that can be stored
int trap(vector<int>& height,int n) {
int prefix[n],suffix[n];
prefix[0]=height[0];
for(int i=1;i<n;i++)
{
prefix[i]=max(prefix[i-1],height[i]);
}
suffix[n-1]=height[n-1];
for(int i=n-2;i>=0;i--)
{
suffix[i]=max(suffix[i+1],height[i]);
}
int sum=0;
for(int i=0;i<n;i++)
{
sum+=min(suffix[i],prefix[i])-height[i];
}
return sum;
}
// Driver code //0,1,0,2,1,0,1,3,2,1,2,1
int main()
{
vector<int>height;
int n,ele;
cout<<"Enter the size of array"<<endl;
cin>>n;
for(int i=0;i<n;i++){
cin>>ele;
height.push_back(ele);
}

cout << trap(height, n);

return 0;
}