FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DSA_Problems/LeetCode/0085_MaximalRectangle.cpp at main · lakshitcodes/DSA_Problems · GitHub
lakshitcodes
/
DSA_Problems
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
DSA_Problems
/
LeetCode
/
0085_MaximalRectangle.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (82 loc) · 2.05 KB
Breadcrumbs
DSA_Problems
/
LeetCode
/
0085_MaximalRectangle.cpp
Copy path
File metadata and controls
84 lines (82 loc) · 2.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#
include
<
bits/stdc++.h
>
using
namespace
std
;
//
Question Link : https://leetcode.com/problems/maximal-rectangle/
class
Solution
{
public:
vector<
int
>
nextSmallerindex
(vector<
int
> &arr,
int
n)
{
stack<
int
> st;
st.
push
(-
1
);
vector<
int
>
ans
(n);
for
(
int
i = n -
1
; i >=
0
; i--)
{
int
curr = arr[i];
while
(st.
top
() != -
1
&& arr[st.
top
()] >= curr)
{
st.
pop
();
}
ans[i] = st.
top
();
st.
push
(i);
}
return
ans;
}
vector<
int
>
prevSmallerindex
(vector<
int
> &arr,
int
n)
{
stack<
int
> st;
st.
push
(-
1
);
vector<
int
>
ans
(n);
for
(
int
i =
0
; i < n; i++)
{
int
curr = arr[i];
while
(st.
top
() != -
1
&& arr[st.
top
()] >= curr)
{
st.
pop
();
}
ans[i] = st.
top
();
st.
push
(i);
}
return
ans;
}
int
largestRectangleArea
(vector<
int
> &heights)
{
int
n = heights.
size
();
vector<
int
>
next
(n);
next =
nextSmallerindex
(heights, n);
vector<
int
>
prev
(n);
int
ans =
0
;
prev =
prevSmallerindex
(heights, n);
for
(
int
i =
0
; i < n; i++)
{
int
l = heights[i];
if
(next[i] == -
1
)
{
next[i] = n;
}
int
b = next[i] - prev[i] -
1
;
ans =
max
(ans, l * b);
}
return
ans;
}
int
maximalRectangle
(vector<vector<
char
>> &matrix)
{
vector<
int
>
height
(matrix[
0
].
size
(),
0
);
int
maxi =
INT_MIN
;
for
(
int
i =
0
; i < matrix.
size
(); i++)
{
for
(
int
j =
0
; j < height.
size
(); j++)
{
if
(matrix[i][j] ==
'
1
'
)
{
height[j]++;
}
else
{
height[j] =
0
;
}
}
maxi =
max
(maxi,
largestRectangleArea
(height));
}
return
maxi;
}
};
Back
|
FazBrowse Home
|
New Git URL