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- ...
随机推荐
- qt study2
- 请爱护你的JTAG烧录口---记录
排除了下载线的问题后,还是不能访问FPGA的JTAG口,那么很有可能你的FPGA芯片的JTAG口已经损坏.此时请用万用表检查TCK,TMS,TDO和Tdi是否和GND短路,如果任何一个信号对地 ...
- 预备作业02:体会做中学(Learning By Doing)
1.很惭愧,我并没有什么技能能强过大家. 2...... 3.我觉得培养一个技能,必须要通过勤勉的练习,认真的学习,还有不断地结合实践. 4.我觉得我学习<程序设计与数据结构>之后应该对程 ...
- ksyun主机挂载ksyun硬盘
1.查看虚拟磁盘的设备号是 /dev/vdc ls /dev/vd*2.格式化块设备,强烈推荐ext4文件系统: mkfs.ext4 /dev/vdc 3.准备挂载点,建立挂载目录.例: mkdir ...
- [ZHOJ1956]vfk的地雷
题目大意: 有$n$个开关,$r$句话. 每个开关$i$有$p_i$的概率被触发,并造成$d_i$的代价. 每个开关至多被触发一次,一句话至多触发一个开关. 每个开关按照顺序被尝试触发. 求期望代价. ...
- ZOJ 2970 Faster, Higher, Stronger
F - Faster, Higher, Stronger Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld &am ...
- git中如何合并某个指定文件?
分支A_bracn和B_branch,只想将A_branch分支的某个文件f.txt合并到B_branch分支上.git checkout A_branch git checkout --p ...
- Ubuntu16.04中安装stlink驱动
系统环境: Vmware12, Ubuntu16.04 Stlink version:v1.4.0 一.安装依赖包: sudo apt-get install libusb-1.0 sudo apt- ...
- better-scroll不生效原因
在vue项目中运用better-scroll插件进行上拉加载的功能时,页面拉不动. html结构: <div class="wrapper" ref="wrappe ...
- Apache的443端口被占用解决方法(转)
今天想做PHP程序,结果启动Apache的时候控制台报443端口被占用.原因是我的虚拟机VMware占用443端口用于连接远程服务器的.其实出现这些状况很正常.因为不同的程序很有可能同时需要一个端口维 ...