-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
42 lines (39 loc) · 1.01 KB
/
Solution.cs
File metadata and controls
42 lines (39 loc) · 1.01 KB
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
42
public class Solution
{
public int MaxSideLength(int[][] mat, int threshold)
{
int m = mat.Length, n = mat[0].Length;
int[,] rowSum = new int[m, n + 1];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
int v = mat[i][j];
rowSum[i, j + 1] = rowSum[i, j] + v;
}
}
for (int k = Math.Min(m, n); k > 0; k--)
{
for (int r = 0; r + k <= m; r++)
{
for (int c = 0; c + k <= n; c++)
{
if (IsValid(r, c, k, rowSum, threshold))
{
return k;
}
}
}
}
return 0;
}
bool IsValid(int r, int c, int k, int[,] rowSum, int threshold)
{
long sum = 0;
for (int i = r; i < r + k; i++)
{
sum += rowSum[i, c + k] - rowSum[i, c];
}
return sum <= threshold;
}
}