Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 955db95

Browse files
committed
Add solution #3070
1 parent 7cd6a7b commit 955db95

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,7 @@
21552155
3067|[Count Pairs of Connectable Servers in a Weighted Tree Network](./solutions/3067-count-pairs-of-connectable-servers-in-a-weighted-tree-network.js)|Medium|
21562156
3068|[Find the Maximum Sum of Node Values](./solutions/3068-find-the-maximum-sum-of-node-values.js)|Hard|
21572157
3069|[Distribute Elements Into Two Arrays I](./solutions/3069-distribute-elements-into-two-arrays-i.js)|Easy|
2158+
3070|[Count Submatrices with Top-Left Element and Sum Less Than k](./solutions/3070-count-submatrices-with-top-left-element-and-sum-less-than-k.js)|Medium|
21582159
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
21592160
3108|[Minimum Cost Walk in Weighted Graph](./solutions/3108-minimum-cost-walk-in-weighted-graph.js)|Hard|
21602161
3110|[Score of a String](./solutions/3110-score-of-a-string.js)|Easy|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 3070. Count Submatrices with Top-Left Element and Sum Less Than k
3+
* https://leetcode.com/problems/count-submatrices-with-top-left-element-and-sum-less-than-k/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer matrix grid and an integer k.
7+
*
8+
* Return the number of submatrices that contain the top-left element of the grid, and have a sum
9+
* less than or equal to k.
10+
*/
11+
12+
/**
13+
* @param {number[][]} grid
14+
* @param {number} k
15+
* @return {number}
16+
*/
17+
var countSubmatrices = function(grid, k) {
18+
const rows = grid.length;
19+
const cols = grid[0].length;
20+
const prefixSum = Array.from({ length: rows + 1 }, () => new Array(cols + 1).fill(0));
21+
22+
for (let i = 1; i <= rows; i++) {
23+
for (let j = 1; j <= cols; j++) {
24+
prefixSum[i][j] = grid[i-1][j-1] + prefixSum[i-1][j]
25+
+ prefixSum[i][j-1] - prefixSum[i-1][j-1];
26+
}
27+
}
28+
29+
let result = 0;
30+
for (let i = 1; i <= rows; i++) {
31+
for (let j = 1; j <= cols; j++) {
32+
if (prefixSum[i][j] <= k) {
33+
result++;
34+
}
35+
}
36+
}
37+
38+
return result;
39+
};

0 commit comments

Comments
 (0)