πDay 5. Histogram Max Rectangular Area π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
You are given a histogram represented by an array arr
, where each element denotes the height of the bars.
Each bar has a width of 1 unit.
Your task is to find the largest rectangular area possible in the given histogram, where the rectangle is formed using contiguous bars.
π Example Walkthrough:
Example 1:
Input:
arr[] = [60, 20, 50, 40, 10, 50, 60]
Output:
100
Explanation:
The maximum area is achieved by picking bars 50 and 60. The area is computed as: $[ \text{(smallest height)} \times \text{(number of picked bars)} = 50 \times 2 = 100 $]
Example 2:
Input:
arr[] = [3, 5, 1, 7, 5, 9]
Output:
15
Explanation:
The maximum area is achieved by picking bars 7, 5, and 9. $[ 5 \times 3 = 15 $]
Example 3:
Input:
arr[] = [3]
Output:
3
Explanation:
The largest area is simply 3 (height 3
, width 1
).
Constraints:
$(1 \leq \text{arr.size()} \leq 10^5)$
$(0 \leq \text{arr[i]} \leq 10^4)$
π― My Approach:
Optimized Monotonic Stack (O(N) Time, O(N) Space)
Use a stack to keep track of indices of increasing heights.
Process each bar once, ensuring an O(N) complexity.
Calculate maximum width dynamically by maintaining left and right boundaries.
Algorithm Steps:
Traverse each element in
arr[]
and use a monotonic increasing stack to store indices.If
arr[i]
is smaller than the top of the stack, compute the largest area with the popped height.The width is determined by checking the previous elements stored in the stack.
Repeat the process until all elements are processed.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), as each bar is processed only once.
Expected Auxiliary Space Complexity: O(N), due to stack storage.
π Solution Code
Code (C++)
class Solution {
public:
int getMaxArea(vector<int>& arr) {
stack<int> s;
int n = arr.size(), res = 0;
for (int i = 0; i <= n; i++) {
while (!s.empty() && (i == n || arr[s.top()] >= arr[i])) {
int h = arr[s.top()];
s.pop();
int w = s.empty() ? i : i - s.top() - 1;
res = max(res, h * w);
}
s.push(i);
}
return res;
}
};
Code (Java)
class Solution {
public int getMaxArea(int[] arr) {
Stack<Integer> s = new Stack<>();
int n = arr.length, res = 0;
for (int i = 0; i <= n; i++) {
while (!s.isEmpty() && (i == n || arr[s.peek()] >= arr[i])) {
int h = arr[s.pop()];
int w = s.isEmpty() ? i : i - s.peek() - 1;
res = Math.max(res, h * w);
}
s.push(i);
}
return res;
}
}
Code (Python)
class Solution:
def getMaxArea(self, arr):
stack, n, res = [], len(arr), 0
for i in range(n + 1):
while stack and (i == n or arr[stack[-1]] >= arr[i]):
h = arr[stack.pop()]
w = i if not stack else i - stack[-1] - 1
res = max(res, h * w)
stack.append(i)
return res
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated