Skip to content

Commit 05b2adf

Browse files
authored
Create main.cpp
1 parent 1c85153 commit 05b2adf

File tree

1 file changed

+96
-0
lines changed
  • 24 - Dynamic Programming Problems/39 - Max Rectangle

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
class Solution {
2+
// Function to find the index of the next smaller element for each element in the array
3+
private:
4+
vector<int> nextSmallerElementIndexes(vector<int> arr, int n){
5+
stack<int> st; // Stack to store indices
6+
vector<int> ans(n); // Vector to store the result
7+
8+
st.push(-1); // Push -1 as the base value for comparison
9+
10+
for(int i = n-1; i >= 0; i--){ // Traverse the array from right to left
11+
int current = arr[i]; // Current element
12+
13+
// Pop elements from the stack until a smaller element is found
14+
while(st.top() != -1 && arr[st.top()] >= current){
15+
st.pop();
16+
}
17+
18+
ans[i] = st.top(); // Store the index of the next smaller element
19+
st.push(i); // Push current index to the stack
20+
}
21+
22+
return ans;
23+
}
24+
25+
// Function to find the index of the previous smaller element for each element in the array
26+
vector<int> prevSmallerElementIndexes(vector<int> arr, int n){
27+
stack<int> st; // Stack to store indices
28+
vector<int> ans(n); // Vector to store the result
29+
30+
st.push(-1); // Push -1 as the base value for comparison
31+
32+
for(int i = 0; i < n; i++){ // Traverse the array from left to right
33+
int current = arr[i]; // Current element
34+
35+
// Pop elements from the stack until a smaller element is found
36+
while(st.top() != -1 && arr[st.top()] >= current){
37+
st.pop();
38+
}
39+
40+
ans[i] = st.top(); // Store the index of the previous smaller element
41+
st.push(i); // Push current index to the stack
42+
}
43+
44+
return ans;
45+
}
46+
47+
// Function to calculate the largest rectangular area that can be formed from the histogram
48+
int largestRectangleArea(vector<int>& heights) {
49+
int n = heights.size(); // Get the number of bars in the histogram
50+
51+
// Get the next smaller element indexes for each bar
52+
vector<int> next(n);
53+
next = nextSmallerElementIndexes(heights, n);
54+
55+
// Get the previous smaller element indexes for each bar
56+
vector<int> prev(n);
57+
prev = prevSmallerElementIndexes(heights, n);
58+
59+
int area = INT_MIN; // Initialize area to the minimum possible value
60+
61+
for(int i = 0; i < n; i++){
62+
int l = heights[i]; // Height of the current bar
63+
64+
// If no next smaller element exists, set it to n (end of the histogram)
65+
if(next[i] == -1) next[i] = n;
66+
67+
// Width is the distance between the previous and next smaller elements
68+
int b = next[i] - prev[i] - 1;
69+
70+
// Calculate the area for the current bar and update the maximum area
71+
int newArea = l * b;
72+
area = max(area, newArea);
73+
}
74+
75+
return area; // Return the maximum area found
76+
}
77+
78+
public:
79+
// Function to calculate the maximum area of a rectangle in a 2D matrix
80+
int maxArea(vector<vector<int>> &mat) {
81+
vector<int> height(mat[0].size(), 0); // Initialize height array with 0s (one for each column)
82+
int maxi = INT_MIN; // Variable to store the maximum area
83+
84+
for(int i = 0; i < mat.size(); i++){ // Traverse each row of the matrix
85+
for(int j = 0; j < height.size(); j++){ // Traverse each column in the row
86+
if(mat[i][j] == 1) height[j]++; // If the element is 1, increment the height
87+
else height[j] = 0; // If the element is 0, reset the height
88+
}
89+
90+
// Calculate the maximum area for the current row using the histogram approach
91+
maxi = max(maxi, largestRectangleArea(height));
92+
}
93+
94+
return maxi; // Return the maximum area found
95+
}
96+
};

0 commit comments

Comments
 (0)