Skip to content

Commit 18c326d

Browse files
authored
Create 3070-count-submatrices-with-top-left-element-and-sum-less-than-k.js
1 parent 492e715 commit 18c326d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var countSubmatrices = function (grid, k) {
7+
const n = grid.length
8+
const m = grid[0].length
9+
const acc = new Array(m + 1).fill(0)
10+
11+
let res = 0
12+
for (let i = 0; i < n; i++) {
13+
const tmp = new Array(m + 1).fill(0)
14+
for (let j = 0; j < m; j++) {
15+
tmp[j + 1] = tmp[j] + grid[i][j]
16+
}
17+
18+
for (let j = 0; j < m; j++) {
19+
acc[j + 1] += tmp[j + 1]
20+
if (acc[j + 1] <= k) {
21+
res += 1
22+
}
23+
}
24+
}
25+
return res
26+
}

0 commit comments

Comments
 (0)