Skip to content

Commit c7138bc

Browse files
authored
Create 1504-count-submatrices-with-all-ones.js
1 parent 83370d8 commit c7138bc

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @return {number}
4+
*/
5+
const numSubmat = function(mat) {
6+
let m = mat.length
7+
let n = mat[0].length
8+
let res = 0
9+
let height = Array(n).fill(0)
10+
for (let i = 0; i < m; i++) {
11+
let st = []
12+
for (let j = 0; j < n; j++) {
13+
if (mat[i][j] == 1) {
14+
height[j]++
15+
} else {
16+
height[j] = 0
17+
}
18+
let sum = 0
19+
while (st.length != 0) {
20+
if (height[st[st.length - 1][0]] < height[j]) break
21+
st.pop()
22+
}
23+
if (st.length != 0) {
24+
sum += height[j] * (j - st[st.length - 1][0]) + st[st.length - 1][1]
25+
} else {
26+
sum += height[j] * (j + 1)
27+
}
28+
st.push([j, sum])
29+
res += sum
30+
}
31+
}
32+
return res
33+
34+
};

0 commit comments

Comments
 (0)