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 ...
随机推荐
- No buffer space available错误解决方案
生产环境,突然爆发问题,网络正常,但是报网络不通错误,重启tomcat立刻好使 开始以为是tomcat的问题,调查了很久,后来加上堆栈日志才发现 简单来说,就是1-4分钟之内,使用了大量的http请求 ...
- mySQL and sqoop for ubuntu
数据的导入导出 ——MySQL & sqoop in Ubuntu 1.完成搭建hadoop集群 2.安装MySQL sudo apt-get install mysql-server mys ...
- 模拟select控件功能
直接上代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- [luoguP1922] 女仆咖啡厅桌游吧(奇奇怪怪的树形DP)
传送门 什么鬼的题? 代码 #include <cstdio> #include <cstring> #include <iostream> #define N 1 ...
- [luoguP1516] 青蛙的约会(扩展欧几里得)
传送门 对于数论只会gcd的我,也要下定决心补数论了 列出方程 (x + t * m) % l = (y + t * n) % l 那么假设 这两个式子之间相差 num 个 l,即为 x + t * ...
- [bzoj1070][SCOI2007]修车[ 网络流]
把每个工人拆成N个点.记为A[i,j]表示第i个工人修倒数第j辆车.每个车跟所有N*M个工人拆出的点连边.流量为1,费用为$time[i,j]*k$.源和每辆车连边,N*M个点和汇连边,流量都为1,费 ...
- Ubuntu 16.04修复PDF默认使用ImageMagick打开无法设置其它默认的问题(默认打开程序设置)
打开:~/.config/mimeapps.list 去掉以下几项: image/pdf=display-im6.desktop image/pdf=display-im6.q16.desktop;d ...
- 3.1-HDLC/PPP
同步串行链路(Serail Point-to-Point Link)的封装 3.1-HDLC/PPP 高级数据链路控制HDLC(High-Level Data Link Control): ...
- C#中方法的详解
访问修饰符 修饰符 返回值类型 方法名(参数列表){ 语句块;} 访问修饰符:所有类成员访问修饰符都可以使用,如果省略访问修饰符,默认是private. 修饰符:在定义方法时修饰符包括virtual( ...
- Python3基础(十二) 学习总结·附PDF
Python是一门强大的解释型.面向对象的高级程序设计语言,它优雅.简单.可移植.易扩展,可用于桌面应用.系统编程.数据库编程.网络编程.web开发.图像处理.人工智能.数学应用.文本处理等等. 在学 ...