LeetCode每日一题(1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold)

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
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值