363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.
Example:
Given matrix = [
  [1,  0, 1],
  [0, -2, 3]
]
k = 2
The answer is 2. Because the sum of rectangle [[0, 1], [-2, 3]] is 2 and 2 is the max number no larger than k (k = 2).
Note:
    The rectangle inside the matrix must have an area > 0.
    What if the number of rows is much larger than the number of columns?
详见:https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/description/

C++:

class Solution {
public:
    int maxSumSubmatrix(vector<vector<int>>& matrix, int k)
    {
        if (matrix.empty() || matrix[0].empty())
        {
            return 0;
        }
        int m = matrix.size(), n = matrix[0].size(), res = INT_MIN;
        int sum[m][n];
        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                int t = matrix[i][j];
                if (i > 0)
                {
                    t += sum[i - 1][j];
                }
                if (j > 0)
                {
                    t += sum[i][j - 1];
                }
                if (i > 0 && j > 0)
                {
                    t -= sum[i - 1][j - 1];
                }
                sum[i][j] = t;
                for (int r = 0; r <= i; ++r)
                {
                    for (int c = 0; c <= j; ++c)
                    {
                        int d = sum[i][j];
                        if (r > 0)
                        {
                            d -= sum[r - 1][j];
                        }
                        if (c > 0)
                        {
                            d -= sum[i][c - 1];
                        }
                        if (r > 0 && c > 0)
                        {
                            d += sum[r - 1][c - 1];
                        }
                        if (d <= k)
                        {
                            res = max(res, d);
                        }
                    }
                }
            }
        }
        return res;
    }
};

参考:https://www.cnblogs.com/grandyang/p/5617660.html

原文地址:https://www.cnblogs.com/xidian2014/p/8847726.html

时间: 2024-08-04 03:41:50

363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K的相关文章

【leetcode】363. Max Sum of Rectangle No Larger Than K

题目描述: Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. 解题思路: 根据题意,寻找二维数组中所有可以组成的矩形中面积不超过k的最大值,所以必须要求出可能组成的矩形的面积并与k比较求出最终结果.这里为了最终不超时,可以在一下方面进行优化: 1.设置一个数组比较当前列(或

Leetcode: Max Sum of Rectangle No Larger Than K

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. Example: Given matrix = [ [1, 0, 1], [0, -2, 3] ] k = 2 The answer is 2. Because the sum of rectangle [[0, 1], [

[LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. Example: Given matrix = [ [1, 0, 1], [0, -2, 3] ] k = 2 The answer is 2. Because the sum of rectangle [[0, 1], [

Max Sum of Rectangle No Larger Than K

public class Solution { public int maxSumSubmatrix(int[][] matrix, int k) { if (matrix.length == 0 || matrix[0].length == 0) { return 0; } int result = Integer.MIN_VALUE; int row = matrix.length, col = matrix[0].length; int m = Math.min(row, col); in

HDU 1003 Max Sum【动态规划求最大子序列和详解 】

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 250714    Accepted Submission(s): 59365 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su

杭电 1003 Max Sum

http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142781    Accepted Submission(s): 33242 Problem Description Given a sequence a[1],a[2],a[3

HDU acm 1003 Max Sum || 动态规划求最大子序列和详解

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 164592    Accepted Submission(s): 38540 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su

HDOJ 1003 Max Sum【MSS】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 158875    Accepted Submission(s): 37166 Problem Description Given a sequence a[1],a[2

杭电(hdu)ACM 1003 Max Sum

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 178139    Accepted Submission(s): 41558 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s