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 ...
随机推荐
- 调用subprocess调用shell命令时屏蔽标准输出
import os, subprocessp = subprocess.Popen(args, stdout = subprocess.PIPE,stderr = subprocess.STDOUT)
- PAT 1130 Infix Expression
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- 单例模式(C#实现)
这是这段时间学习设计模式的时候的源代码. 单例(单件)模式的五种实现. 通过一个计数器的例子调用验证一下. 把下面的代码直接拷进vs下,运行就可以了.(控制台应用程序) 以后把剩余的设计模式有空儿就粘 ...
- KMP超强模板贴一份
)== ) { ); next[]=; ; ;i<=n;i++) { ]!=str[i]) j=next[j]; ...
- [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 204 Solved: 129[Submit][Status][Discuss] Descriptio ...
- SSH三种框架及表示层、业务层和持久层的理解(转)
Struts(表示层)+Spring(业务层)+Hibernate(持久层) SSH:Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts:Struts是一个表示 ...
- centos7 mysql安装与用户设置
1.环境:Centos 7.0 64位2.mysql版本:5.73.安装:https://dev.mysql.com/doc/refman/5.7/en/installing.html3.1.创建my ...
- 最小生成树 I - Agri-Net
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [codevs 1183][泥泞的道路(二分+spfa)
题目:http://dev.codevs.cn/problem/1183/ 分析:这个和最优比率生成树很像,都可以二分答案的,只不过判定方面一个是求是否有最短路径,一个是求是否有生成树.假设等待判定的 ...