From 7f04bb5f36f7dc86c391e8dcc9439204446666e5 Mon Sep 17 00:00:00 2001 From: ashish01012001 <108181303+ashish01012001@users.noreply.github.com> Date: Tue, 10 Oct 2023 09:45:02 +0530 Subject: [PATCH 1/4] Added a new problem (Sum of Subarray Minimums) and added a solution to it in cpp --- codes/cpp/sum_of_subarray_minimums.cpp | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 codes/cpp/sum_of_subarray_minimums.cpp diff --git a/codes/cpp/sum_of_subarray_minimums.cpp b/codes/cpp/sum_of_subarray_minimums.cpp new file mode 100644 index 0000000..f95aa34 --- /dev/null +++ b/codes/cpp/sum_of_subarray_minimums.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +class Solution { +public: + int sumSubarrayMins(vector& arr) { + int n = arr.size(); + int mod = 1e9 + 7; + stack st; + vector 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 v = {3,1,2,4}; + Solution ob; + cout << (ob.sumSubarrayMins(v)); + return 0; +} \ No newline at end of file From 79b1e39259b4a1dc687a96a8bd87c0b38264dbb4 Mon Sep 17 00:00:00 2001 From: ashish01012001 <108181303+ashish01012001@users.noreply.github.com> Date: Tue, 10 Oct 2023 09:46:59 +0530 Subject: [PATCH 2/4] Edited README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 953596d..230b839 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,8 @@ | Longest Valid Parentheses | [LeetCode](https://leetcode.com/problems/longest-valid-parentheses/) | [YouTube](https://www.youtube.com/watch?v=VdQuwtEd10M) | [Java](./codes/java/LongestValidParentheses.java) | | 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) | +|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) From 50938db3423691b1da48a73142ab63d8f87c75b0 Mon Sep 17 00:00:00 2001 From: ashish01012001 <108181303+ashish01012001@users.noreply.github.com> Date: Tue, 10 Oct 2023 11:30:01 +0530 Subject: [PATCH 3/4] Added trapping rain water problem --- codes/cpp/trapping_rainwater.cpp | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 codes/cpp/trapping_rainwater.cpp diff --git a/codes/cpp/trapping_rainwater.cpp b/codes/cpp/trapping_rainwater.cpp new file mode 100644 index 0000000..ea2db24 --- /dev/null +++ b/codes/cpp/trapping_rainwater.cpp @@ -0,0 +1,43 @@ +// C++ implementation of the approach + +#include +#include +using namespace std; + +// Function to return the maximum +// water that can be stored +int trap(vector& height,int n) { + int prefix[n],suffix[n]; + prefix[0]=height[0]; + for(int i=1;i=0;i--) + { + suffix[i]=max(suffix[i+1],height[i]); + } + int sum=0; + for(int i=0;iheight; + int n,ele; + cout<<"Enter the size of array"<>n; + for(int i=0;i>ele; + height.push_back(ele); + } + + cout << trap(height, n); + + return 0; +} \ No newline at end of file From d70aa94f1e7a414fa38e1a9bfcdac37b9d9b5503 Mon Sep 17 00:00:00 2001 From: ashish01012001 <108181303+ashish01012001@users.noreply.github.com> Date: Tue, 10 Oct 2023 11:30:22 +0530 Subject: [PATCH 4/4] Edited README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 230b839..5f8ec49 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ | 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)