max-sum-of-sub-matrix-no-larger-than-k
根据上一篇文章提到的参考文档:
https://leetcode.com/discuss/109749/accepted-c-codes-with-explanation-and-references
我实现的解法,用了一个sum_max,不再另设res。
class Solution {
public:
int maxSumSubmatrix(vector<vector<int>>& matrix, int K) {
int sum_max = INT_MIN;
int rlen = matrix.size();
int clen = matrix[].size();
for (int i=; i<clen; i++) {
vector<int> tmp_vec(rlen, );
for (int j=i; j<clen; j++) {
for (int k=; k<rlen; k++) {
tmp_vec[k] += matrix[k][j];
}
set<int> cur_max_set;
int cur_max = ;
for (int k=; k<rlen; k++) {
cur_max += tmp_vec[k];
if (cur_max <= K) {
sum_max = max(sum_max, cur_max);
}
set<int>::iterator iter = cur_max_set.lower_bound(cur_max - K);
if (iter != cur_max_set.end()) {
sum_max = max(sum_max, cur_max-*iter);
}
cur_max_set.insert(cur_max);
}
}
}
return sum_max;
}
};
|
27 / 27 test cases passed.
|
Status:
Accepted |
|
Runtime: 1016 ms
|
max-sum-of-sub-matrix-no-larger-than-k的更多相关文章
- HDU-1003 Max Sum(动态规划,最长字段和问题)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- [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 ...
- 【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 ...
- 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 ...
- 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 ...
- 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 最大矩阵和不超过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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...
随机推荐
- spring boot配置使用fastjson
一.前言 spring boot默认使用jackson来操作json数据,相比于jackson,fastjson更好用,功能也强大,所以这里记录一下在spring boot中配置使用fastjson的 ...
- tensorflow初始化参数内存占满问题
最近使用tensorflow构建了一个浅层的网络,初始化参数的时候发现两个1080ti 22G的显存马上占满 后来发现解决办法为:在创建session的时候把显存设置为自适应即可,如下 config ...
- (三)宏 __cplusplus C/C++混合编程
前面一些篇目的内容作为一个在校生,很少用到,可能工作的人会见得多一点,但是第一次整体性的学习还是不希望有落下的东西,虽然不常用但至少要有个印象 那么就进入第三篇<宏 __cplusplus> ...
- python流行的原因
12~14年是云计算最火的几年,大批创业公司和巨头挤破头地进军云计算领域,大家都在做IAAS,最著名的云计算开源平台OpenStack 就是基于Python 开发的,为此催生出不少Python 岗位 ...
- NetCore+Dapper WebApi架构搭建(四):仓储的依赖注入
上一节我们讲到实体,仓储接口和仓储接口的实现需要遵循约定的命名规范,不仅是规范,而且为了依赖注入,现在我们实现仓储的依赖注入 在NetCore WebApi项目中新添加一个文件夹(Unit),当然你也 ...
- ubuntu python opencv3 cv2.cv2 has no attribute 'face' 'cv2.face' has no attribute 'createEigenFaceRecognizer'
学习opencv过程中遇到错误: 1 cv2.cv2 has no attribute 'face' 经过一顿查,,,各种走弯路 最后一下子就解决了: pip install opencv-pyth ...
- QT学习笔记3:QT中语法说明
一.Qt 类中Q_OBJECT的作用 QObject 是所有Qt对象的基类. QObject 是Qt模块的核心.它的最主要特征是关于对象间无缝通信的机制:信号与槽.使用connect()建立信号到槽的 ...
- UVALive 4425 Another Brick in the Wall 暴力
C - Another Brick in the Wall Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & ...
- 前端换mac可以参考搭一下简单的环境
1. 安装brew套件管理器 安装向导请点击,注意的地方,mac必须先设置一个密码.装好之后就可以安装各种套件. 2. 安装nvm管理node版本 brew install nvm 安装完成之后nvm ...
- 几张图理解Roll, Pitch, Yaw的含义
Roll:翻滚 Pitch:俯仰 Yaw:偏航 有时候不知道它到底绕着哪个轴旋转得到的角,一个比较容易的记法是根据字母的排列顺序PRY分别对应XYZ轴进行旋转得到的角,即: Pitch是绕 ...