Skip to content

Commit f1d9304

Browse files
authored
Merge pull request #1249 from TheCodeVenturer/thecodeventurer
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area
2 parents 64115b8 + c2ee514 commit f1d9304

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//Program Author : TheCodeVenturer [Niraj Modi]
2+
/*
3+
Problem Definition:
4+
Dynamic Programming: Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area in Python
5+
Approach:
6+
This Problem can be termed as updated version of Largest Rectangle in Histogram
7+
If you are given a binary matrix then you can use no.of rows together with one as height
8+
Like
9+
0 1 1 0
10+
1 1 1 1
11+
1 1 1 1
12+
1 1 0 0
13+
If you can Visualise then it will be clear that for
14+
first row the histogram row is like [0,1,1,0]
15+
second row the histogram row is like [1,2,2,1]
16+
third row the histogram row is like [2,3,3,2]
17+
fourth row the histogram row is like [3,4,0,0]
18+
then by using a monotonic stack for each row we can get The Largest Rectangle in the Binary Matrix
19+
20+
we are taking here a row list which keeps track of current height of a particular column . Here, ShiftRow
21+
we are also using a solution variable to keep track of largest rectangle
22+
then first we will iterate through each row
23+
and inside each iteration we will go and look for the particular element of that row matrix[i][j]
24+
if it is 1 then will increase size of jth entry in the shiftRow else will convert it to zero
25+
next will initialize an empty Stack [Monotonic]
26+
next we will iterate through the shiftRow and will first check for the list is not empty and (it's top element is greater than or equal to current element or value of current column is equal to row size)
27+
then will store it's height from the current row array and will update width of the rectangle with stack's top element and will finally update the sol
28+
and will insert the element to the stack
29+
Complexity:
30+
Time Complexity: O(rows * col) for for traversing through each elements of the array
31+
Here in each iteration we are doint three times O(n) => O(3n) ~ O(n)
32+
Space Complexity: O(n) for the shiftRow and O(n) for the stack we are using => O(2n) ~ O(n)
33+
Sample input/outputs:
34+
Example 1:
35+
Input: [[0,1,1,0],[1,1,1,1],[1,1,1,1],[1,1,0,0]]
36+
Output: 8
37+
38+
Example 2:
39+
Input: [[0,1,1],[1,1,1],[0,1,1]]
40+
Output: 6
41+
*/
42+
var maxArea = function (matrix, rows, cols) {
43+
let shiftRow = new Array(cols).fill(0); //initialising the row which update after each iteration
44+
var sol = 0;
45+
for (let row of matrix) {
46+
for (let i = 0; i < row.length; i++) {
47+
// Updating the shiftRow if value of ele is 1 => ShiftRow[i] <- shiftRow[i]+1
48+
// else shiftRow[i]=0
49+
var ele = row[i];
50+
if (ele == 1) shiftRow[i]++;
51+
else shiftRow[i] = 0;
52+
}
53+
st = [];
54+
for (let i = 0; i < cols + 1; i++) {
55+
while (st.length > 0 &&(i == cols || shiftRow[st[st.length - 1]] >= shiftRow[i])) {
56+
//checking TOS st.length-1
57+
height = shiftRow[st[st.length - 1]]; //for getting height of Current index
58+
st.pop();
59+
width = i; // setting width to i as it is only smallest from beginning
60+
if (st.length > 0) width = i - st[st.length - 1] - 1; // updating width is stack is not empty as it is not the smallest element
61+
sol = Math.max(height * width, sol); // Updating the sol
62+
}
63+
st.push(i); // Pushing the Element's index to the stack
64+
}
65+
}
66+
return sol;
67+
};
68+
69+
var matrix = [
70+
[0, 1, 1, 0],
71+
[1, 1, 1, 1],
72+
[1, 1, 1, 1],
73+
[1, 1, 0, 0],
74+
];
75+
console.log(maxArea(matrix, 4, 4));

0 commit comments

Comments
 (0)