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

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

  1. 363. Max Sum of Rectangle No Larger Than K

    /* * 363. Max Sum of Rectangle No Larger Than K * 2016-7-15 by Mingyang */ public int maxSumSubmatri ...

  2. [LeetCode] 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 s ...

  3. 【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 ma ...

  4. 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...

  5. 第十三周 Leetcode 363. Max Sum of Rectangle No Larger Than K(HARD)

    Leetcode363 思路: 一种naive的算法就是枚举每个矩形块, 时间复杂度为O((mn)^2), 可以做少许优化时间复杂度可以降低到O(mnnlogm), 其中m为行数, n为列数. 先求出 ...

  6. [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 s ...

  7. 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 s ...

  8. 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 s ...

  9. [Swift]LeetCode363. 矩形区域不超过 K 的最大数值和 | 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 s ...

随机推荐

  1. HTML-js 压缩上传的图片方法(默认上传的是file文件)

    //压缩图片方法 function compressImg(file,callback){ var src; var fileSize = parseFloat(parseInt(file['size ...

  2. 关于js中的事件委托小案例

    需求:页面上有一个按钮,和一个空的ul,要求点击按钮,会给ul中动态添加li元素,然后,点击动态添加的元素,在控制台上输出,这是第几个元素 <ul> </ul> <but ...

  3. BZOJ 2274 [Usaco2011 Feb]Generic Cow Protests

    [题解] 很容易可以写出朴素DP方程f[i]=sigma f[j] (sum[i]>=sum[j],1<=j<=i).  于是我们用权值树状数组优化即可. #include<c ...

  4. 数据库删除数据表重复数据,只留下ID较小的行

    删除表中重复数据,留下ID比较小的行 delete from 表 where [重复字段] in (select [重复字段] from 表 group by 字段 having count([字段] ...

  5. nyoj_528_找球号(三)_201404152050

    找球号(三) 时间限制:2000 ms  |  内存限制:3000 KB 难度:2   描述 xiaod现在正在某个球场负责网球的管理工作.为了方便管理,他把每个球都编了号,且每个编号的球的总个数都是 ...

  6. P1656 炸铁路 洛谷

    https://www.luogu.org/problem/show?pid=1656 题目描述 因为某国被某红色政权残酷的高压暴力统治.美国派出将军uim,对该国进行战略性措施,以解救涂炭的生灵. ...

  7. 10、Java并发性和多线程-线程安全与不可变性

    以下内容转自http://ifeve.com/thread-safety-and-immutability/: 当多个线程同时访问同一个资源,并且其中的一个或者多个线程对这个资源进行了写操作,才会产生 ...

  8. 使用jcaptcha插件生成验证码

    1.从官网http://jcaptcha.sourceforge.net/下载插件.将对应jar包导入到lib文件夹下 2.创建一个CaptchaServiceSingleton类用来获取jcaptc ...

  9. Windows Azure 故障转移模式及高可用个模式探讨!

     眼下国内非常多用户对于云服务的可用性存在误解,什么样子的误解呢?比方某云服务商,在华南某地有一个机房,在华东有一个机房. 这个客户就提到一个需求,你提供的99%可用性的概念是什么意思呢?是不是我 ...

  10. 发现所有的字都被加上了 <font> 标签,导致样式全部错乱

    经检查,发现我的浏览器默认打开了翻译软件!!!!!!!