363. Max Sum of Rectangle No Larger Than K
Question
Given an
m x n
matrixmatrix
and an integerk
, return the max sum of a rectangle in the matrix such that its sum is no larger thank
.It is guaranteed that there will be a rectangle with a sum no larger than
k
.
Solution
前缀和,计算矩阵中每个位置对应的方形的和。
遍历方形的两个对角线上的点。
其面积等于大块加小块的面积减去两个长方形的面积。
如果面积有小于k的,则记录其最大值并返回。
Code
1 | class Solution { |
363. Max Sum of Rectangle No Larger Than K
https://xuanhe95.github.io/2022/08/27/363-Max-Sum-of-Rectangle-No-Larger-Than-K/