-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathCount Submatrices With All Ones.java
39 lines (34 loc) · 1.24 KB
/
Count Submatrices With All Ones.java
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
// Runtime: 7 ms (Top 78.37%) | Memory: 45.40 MB (Top 34.13%)
class Solution {
private int n;
private int res = 0;
public int numSubmat(int[][] mat) {
this.n = mat[0].length;
// dp[j] : the height (number of consecutive '1's) of column j
int[] dp = new int[n];
for (int i = 0; i < mat.length; i++) {
// calculating (updating) heights
for (int j = 0; j < n; j++) {
dp[j] = mat[i][j] == 1 ? dp[j] + 1 : 0;
}
enumerateRowByMinHeight(dp);
}
return res;
}
public void enumerateRowByMinHeight(int[] dp) {
// monotonic stack storing indices : for index p < q in stack, dp[p] < dp[q]
Deque<Integer> stack = new LinkedList<>();
stack.offerLast(-1);
for (int j = 0; j < n; j++) {
while (stack.peekLast() != -1 && dp[stack.peekLast()] >= dp[j]) {
int idx = stack.pollLast();
res += dp[idx] * (idx - stack.peekLast()) * (j - idx);
}
stack.offerLast(j);
}
while (stack.peekLast() != -1) {
int idx = stack.pollLast();
res += dp[idx] * (idx - stack.peekLast()) * (n - idx);
}
}
}