363. Max Sum of Rectangle No Larger Than K Hard

@problem@discussion
#Array#Binary Search#Matrix#Prefix Sum#Ordered Set



1/**
2 * [363] Max Sum of Rectangle No Larger Than K
3 *
4 * Given an m x n matrix matrix and an integer k, return the max sum of a rectangle in the matrix such that its sum is no larger than k.
5 * It is guaranteed that there will be a rectangle with a sum no larger than k.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/18/sum-grid.jpg" style="width: 255px; height: 176px;" />
9 * Input: matrix = [[1,0,1],[0,-2,3]], k = 2
10 * Output: 2
11 * Explanation: Because the sum of the blue rectangle [[0, 1], [-2, 3]] is 2, and 2 is the max number no larger than k (k = 2).
12 * 
13 * Example 2:
14 * 
15 * Input: matrix = [[2,2,-1]], k = 3
16 * Output: 3
17 * 
18 *  
19 * Constraints:
20 * 
21 * 	m == matrix.length
22 * 	n == matrix[i].length
23 * 	1 <= m, n <= 200
24 * 	-100 <= matrix[i][j] <= 100
25 * 	-10^5 <= k <= 10^5
26 * 
27 *  
28 * Follow up: What if the number of rows is much larger than the number of columns?
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/
34// discuss: https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn max_sum_submatrix(matrix: Vec<Vec<i32>>, k: i32) -> i32 {
40        0
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_363() {
52    }
53}
54


Back
© 2025 bowen.ge All Rights Reserved.