Given a m x n matrix mat and an integer threshold, return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.
Example 1:
Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
Output: 2
Explanation: The maximum side length of square with sum less than 4 is 2 as shown.
Example 2:
Input: mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1
Output: 0
Constraints:
- m == mat.length
- n == mat[i].length
- 1 <= m, n <= 300
- 0 <= mat[i][j] <= 104
- 0 <= threshold <= 105
先做 row prefix sum, 然后做 rectangle prefix sum, 最后根据 rectangle prefix sum 计算出任意 square 的 sum, 如果 sum <= threshold 则记录其边长, 在这些边长里面取最大值
impl Solution {
pub fn max_side_length(mat: Vec<Vec<i32>>, threshold: i32) -> i32 {
let mut prefix_sum: Vec<Vec<i32>> = mat
.into_iter()
.map(|row| {
let mut sum = row
.into_iter()
.scan(0, |s, v| {
*s += v;
Some(*s)
})
.collect::<Vec<i32>>();
sum.insert(0, 0);
sum
})
.collect();
prefix_sum.insert(0, vec![0; prefix_sum[0].len()]);
for i in 1..prefix_sum.len() {
for j in 1..prefix_sum[0].len() {
prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j];
}
}
let mut ans = 0;
for top in 1..prefix_sum.len() {
for left in 1..prefix_sum[0].len() {
for length in 0..(prefix_sum.len() - top).min(prefix_sum[0].len() - left) {
let bottom = top + length;
let right = left + length;
let sum = prefix_sum[bottom][right]
- prefix_sum[bottom][left - 1]
- prefix_sum[top - 1][right]
+ prefix_sum[top - 1][left - 1];
if sum <= threshold {
ans = ans.max(length as i32 + 1);
}
}
}
}
ans
}
}