-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.js
More file actions
33 lines (31 loc) · 949 Bytes
/
Copy pathsolution.js
File metadata and controls
33 lines (31 loc) · 949 Bytes
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
/**
* @param {number[][]} mat
* @returns {number}
*/
class Solution {
maxArea(mat) {
if (!mat || mat.length === 0 || mat[0].length === 0) return 0;
const n = mat.length;
const m = mat[0].length;
const heights = new Array(m).fill(0);
let maxArea = 0;
for (let i = 0; i < n; i++) {
// update histogram heights
for (let j = 0; j < m; j++) {
heights[j] = mat[i][j] === 1 ? heights[j] + 1 : 0;
}
// largest rectangle in histogram using stack
const st = [];
for (let j = 0; j <= m; j++) {
const cur = j === m ? 0 : heights[j]; // sentinel
while (st.length > 0 && heights[st[st.length - 1]] > cur) {
const h = heights[st.pop()];
const width = st.length === 0 ? j : j - st[st.length - 1] - 1;
maxArea = Math.max(maxArea, h * width);
}
st.push(j);
}
}
return maxArea;
}
}