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
363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K的更多相关文章
- 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 ...
- [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 ...
- 【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 ...
- 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...
- 第十三周 Leetcode 363. Max Sum of Rectangle No Larger Than K(HARD)
Leetcode363 思路: 一种naive的算法就是枚举每个矩形块, 时间复杂度为O((mn)^2), 可以做少许优化时间复杂度可以降低到O(mnnlogm), 其中m为行数, n为列数. 先求出 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- 3.8.5 多重选择:switch语句
在处理多个选项时,使用if/else结构显得有些笨拙. Scanner in = new Scanner(System.in); Syste ...
- 【Codeforces 427C】Checkposts
[链接] 我是链接,点我呀:) [题意] 环里面的点只需要一个点就能全都保护 问你最少需要多少花费以及最少的点才能将所有的点都保护 [题解] 有向图的强连通分量求出所有的联通分量 显然每个联通分量里面 ...
- Jquery 实现表单提交按钮变灰,防止多次点击提交重复数据
表单提交时候我们应该控制提交按钮,不能点击多次进行数据的重复提交.要不然就会有冗余的重复的数据在系统中,造成系统出现数据垃圾.jQuery很简单的就可以实现对表单提交按钮控制,下面就是相关的例子和代码 ...
- J - Invitation Cards 最短路
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- BAT经典面试题,深入理解Java内存模型JMM
Java 内存模型 Java 内存模型(JMM)是一种抽象的概念,并不真实存在,它描述了一组规则或规范,通过这组规范定义了程序中各个变量(包括实例字段.静态字段和构成数组对象的元素)的访问方式.试图屏 ...
- mysql模糊查询语句
select * from tbl_actor where first_char like 'p%' order by first_char;
- 怎样用EA设计ER图
我们开发系统从文档開始,而EA就是替我们开发文档的好工具.结束了我们从概念设计到逻辑设计中的非常多问题.完好我们的文档. 如今就给大家说说如何在EA中设计概念模型ER图: 首先打开EA-"新 ...
- 使用Tomcat的一些经验和心得
如今将使用Tomcat的一些经验和心得写到这里.作为记录和备忘.假设有朋友看到,也请指教. 1.首先是Tomcat的获取和安装. 获取当然得上Apache的官方站点下载,开源免费.并且带宽也足够.下载 ...
- Tomcat PK Resin
特征 Tomcat Resin 所属公司 Apache CAUCHO 用户数 多 少 可參考文档 多 少 与Eclipse集成复杂度 适中 较复杂. Eclipse下调试开发 简便 复杂.更新类后会自 ...
- 5分钟APIG实战: 使用Rust语言快速构建API能力开放
序言:Rust语言简介 参与过C/C++大型项目的同学可能都经历过因为Null Pointer.Memory Leak等问题“被” 加班了不知道多少个晚上.别沮丧,你不是一个人,Mozilla Fir ...