-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxiumRectAreaLecture55.cpp
More file actions
51 lines (51 loc) · 1.24 KB
/
Copy pathMaxiumRectAreaLecture55.cpp
File metadata and controls
51 lines (51 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <vector>
#include <iostream>
#include <algorithm>
#include <climits>
#include <stack>
using namespace std;
//https://bit.ly/33HFz61
int RectArea(vector<int>& heights)
{
stack<int> st;
int i = 0,ans = 0;
int n = heights.size();
heights.push_back(0);
// considered so that when i == n, the whole stack is considered in the area.
for(int i = 0;i <= n;i++)
{
while(!st.empty() && heights[st.top()] > heights[i])
{
int j = st.top();
st.pop();
int h = heights[j];
if(st.empty())
{
ans = max(ans, h * i);
}
else
{
int width = i - st.top() - 1;
ans = max(ans,width * h);
}
}
st.push(i);
}
return ans;
}
int maximalAreaOfSubMatrixOfAll1(vector<vector<int>> &mat, int n, int m)
{
int maxArea = 0;
vector<int> height(m,0);
for(int i = 0;i < n;i++)
{
for(int j = 0;j < m;j++)
{
if(mat[i][j] == 1) height[j]++;
else height[j] = 0;
}
int area = RectArea(height);
maxArea = max(maxArea,area);
}
return maxArea;
}