-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
41 lines (34 loc) · 929 Bytes
/
Solution.cs
File metadata and controls
41 lines (34 loc) · 929 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
34
35
36
37
38
39
40
41
public class Solution
{
public int CountSubmatrices(int[][] grid, int k)
{
int m = grid.Length, n = grid[0].Length;
int[][] fn = new int[m][];
for (int i = 0; i < m; i++)
{
fn[i] = new int[n];
}
int count = 0;
fn[0][0] = grid[0][0];
if (fn[0][0] <= k) count++;
for (int i = 1; i < m; i++)
{
fn[i][0] = fn[i - 1][0] + grid[i][0];
if (fn[i][0] <= k) count++;
}
for (int j = 1; j < n; j++)
{
fn[0][j] = fn[0][j - 1] + grid[0][j];
if (fn[0][j] <= k) count++;
}
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
fn[i][j] = fn[i - 1][j] + fn[i][j - 1] - fn[i - 1][j - 1] + grid[i][j];
if (fn[i][j] <= k) count++;
}
}
return count;
}
}