From 20d227feb89f79aee0d9efb16edc2c57eca1caf4 Mon Sep 17 00:00:00 2001 From: DharmaWarrior <97218268+DharmaWarrior@users.noreply.github.com> Date: Wed, 5 Oct 2022 02:57:09 +0530 Subject: [PATCH] Solutions for 2 questions on leetcode The Questions are : 1st -> Largest Rectangle in Histogram https://leetcode.com/problems/largest-rectangle-in-histogram/ 2nd -> Valid Parenthesis https://leetcode.com/problems/valid-parentheses/ --- .../LargestRectangleInHistogram.md | 62 +++++++++++++++++++ LeetCode Solutions/ValidParenthesis.md | 42 +++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 LeetCode Solutions/LargestRectangleInHistogram.md create mode 100644 LeetCode Solutions/ValidParenthesis.md diff --git a/LeetCode Solutions/LargestRectangleInHistogram.md b/LeetCode Solutions/LargestRectangleInHistogram.md new file mode 100644 index 00000000..398c592d --- /dev/null +++ b/LeetCode Solutions/LargestRectangleInHistogram.md @@ -0,0 +1,62 @@ +class Solution { +private: + vector nextSmallerElement(vector arr, int n) { + stack s; + s.push(-1); + vector ans(n); + + for(int i=n-1; i>=0 ; i--) { + int curr = arr[i]; + while(s.top() != -1 && arr[s.top()] >= curr) + { + s.pop(); + } + //ans is stack ka top + ans[i] = s.top(); + s.push(i); + } + return ans; + } + + vector prevSmallerElement(vector arr, int n) { + stack s; + s.push(-1); + vector ans(n); + + for(int i=0; i= curr) + { + s.pop(); + } + //ans is stack ka top + ans[i] = s.top(); + s.push(i); + } + return ans; + } + +public: + int largestRectangleArea(vector& heights) { + int n= heights.size(); + + vector next(n); + next = nextSmallerElement(heights, n); + + vector prev(n); + prev = prevSmallerElement(heights, n); + + int area = INT_MIN; + for(int i=0; i s; + for(int i=0; i