Skip to content

Commit d6173de

Browse files
Update maximal_rectangle.cpp
1 parent 292696e commit d6173de

File tree

1 file changed

+50
-66
lines changed

1 file changed

+50
-66
lines changed

maximal_rectangle.cpp

Lines changed: 50 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,62 @@
11
class Solution {
22
public:
3-
/*
4-
int dfs(vector<vector<char>>& matrix, int i, int j){
5-
if(i < 0 or j < 0 or i >= matrix.size() or j >= matrix[i].size() or matrix[i][j] == '0')
6-
return 0;
7-
else{
8-
matrix[i][j] = '0';
9-
return 1 + dfs(matrix, i-1, j) + dfs(matrix, i+1, j) + dfs(matrix, i, j + 1) + dfs(matrix, i, j - 1);
10-
}
11-
}*/
12-
13-
int largestRectangleArea(vector<int>& heights) {
3+
int largestRectangleArea(vector<int>& heights) {
4+
5+
int length = heights.size();
6+
int verticalHeight, leftBound, horizontalWidth, currentArea, maxArea = -1, currentIndex;
7+
stack<int> indexes;
148

15-
/*
16-
if(heights.size() == 0) return 0;
179

18-
stack<int> st;
19-
int max_rect_area = INT_MIN;
20-
int index, curr_area, i;
21-
22-
for(i=0; i<=heights.size(); i++){
23-
int cheight = i == heights.size() ? 0 : heights[i];
24-
if(st.empty() || cheight >= heights[st.top()]){
25-
st.push(i++);
26-
}
27-
else{
28-
index = st.top();
29-
st.pop();
30-
*/
31-
/*
32-
height[index] = max_rect_seen_so_far
33-
if stack is empty..
34-
there is no earlier start that is present
35-
this means... the current rect is the only start and end..
36-
hence, curr_area = height[index] * i;
10+
for(int i = 0; i < length; i++) {
11+
currentIndex = i;
12+
13+
//found a decreasing sequence - calculate maxArea
14+
while(!indexes.empty() and heights[currentIndex] <= heights[indexes.top()]) {
3715

38-
if stack is not empty..
39-
there is earlier start that is present
40-
this means... the current rect has the end..
41-
hence, curr_area = height[index] * (i - s.top() - 1)
42-
*/
43-
/*
44-
int breadth = st.empty() ? i : i - st.top() - 1;
45-
max_rect_area = max(max_rect_area, heights[index] * breadth);
16+
verticalHeight = heights[indexes.top()];
17+
indexes.pop();
18+
19+
if(indexes.empty())
20+
horizontalWidth = currentIndex;
21+
else {
22+
leftBound = indexes.top();
23+
horizontalWidth = currentIndex - leftBound - 1;
24+
}
25+
26+
currentArea = verticalHeight * horizontalWidth;
27+
28+
if(currentArea > maxArea)
29+
maxArea = currentArea;
4630
}
31+
32+
//when its strictly monotonically increasing sequence of heights
33+
indexes.push(currentIndex);
4734
}
4835

49-
/*
50-
while(!st.empty()){
51-
index = st.top();
52-
st.pop();
53-
curr_area = heights[index] * (st.empty() ? i : i - st.top() - 1);
54-
max_rect_area = max(curr_area, max_rect_area);
55-
}*/
56-
//return max_rect_area;
57-
int ans = 0;
58-
int len = heights.size();
59-
if(len == 0){
60-
return 0;
61-
}
62-
stack<int> st;
63-
for(int i = 0; i <= len;){
64-
int h = (i == len ? 0 : heights[i]);
65-
if(st.empty() || heights[st.top()] <= h){
66-
st.push(i++);
67-
}else{
68-
int tmp = st.top();
69-
st.pop();
70-
int l = (st.empty() ? i : i - st.top() - 1);
71-
ans = max(ans, l * heights[tmp]);
36+
//if the array only has increasing sequence of elements
37+
if(!indexes.empty()) currentIndex++; // you need to be out of bounds at this point to get the
38+
while(!indexes.empty()) {
39+
40+
verticalHeight = heights[indexes.top()];
41+
indexes.pop();
42+
43+
if(indexes.empty())
44+
horizontalWidth = currentIndex;
45+
else {
46+
leftBound = indexes.top();
47+
horizontalWidth = currentIndex - leftBound - 1;
48+
}
49+
50+
currentArea = verticalHeight * horizontalWidth;
51+
52+
if(currentArea > maxArea)
53+
maxArea = currentArea;
7254
}
73-
}
74-
return ans;
55+
56+
return maxArea;
57+
7558
}
59+
7660

7761
int maximalRectangle(vector<vector<char>>& matrix) {
7862
if(matrix.size() == 0) return 0;
@@ -91,4 +75,4 @@ class Solution {
9175
}
9276
return max_area;
9377
}
94-
};
78+
};

0 commit comments

Comments
 (0)