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- ...
随机推荐
- (转)看懂UML类图
转自:http://design-patterns.readthedocs.io/zh_CN/latest/read_uml.html 这里不会将UML的各种元素都提到,我只想讲讲类图中各个类之间的关 ...
- react篇章-React State(状态)-数据自顶向下流动
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- CentOS日志的简单介绍
在CentOS7中,系统的日志消息由两个服务负责处理:system-journald和rsyslog. (1).常见的日志及作用 /var/log目录里存放了一些特定于系统和服务的日志文件,由rsys ...
- Ajax与传统Web开发的区别
基本概念 1.1,Ajax AJAX:即“Asynchronous Javascript And XML”(异步的JavaScript和XML),是指一种创建交互式网页应用的网页开发技术,尤其是在一种 ...
- [flask]flask_login模块,session及其他
读flask源码的时候,有一点一直到现在都没有一个清晰的概念,比如四个全局变量g,current_app,session,request是怎么做到的 按照查到的资料里面的说法,为了不至于每次都主动调用 ...
- [BZOJ4883][Lydsy1705月赛]棋盘上的守卫(Kruskal)
对每行每列分别建一个点,问题转为选n+m条边,并给每条边选一个点覆盖,使每个点都被覆盖.也就是最小生成环套树森林. 用和Kruskal一样的方法,将边从小到大排序,若一条边被选入后连通块仍然是一个环套 ...
- 【20181031T1】一串数字【分解质因数+贪心】
题面 [错解] 立方就是所有质因子次数都是3的倍数嘛 发现1e5的三次根很小,可以枚举所有和这个数乘起来是完全立方数的(flag*1) 然后--连条边跑最大独立集? 不对啊是NP问题(实际上是个二分图 ...
- 如何离线安装GitHub for windows?
此文献给xp用户和被墙用户. 今天群里(GitHub家园 225932282)有人说GitHub for windows安装不上,错误提示如下,看了下感觉应该是被墙了,我试了试下面的网址,没问题,所以 ...
- Python168的学习笔记8
#coding:utf8 #斐波那契数列,第三项起,每一项都等于前两项之和 def memo(func): cache = {}#闭包 def wrap(*args): if args not in ...
- hdoj 5113 Black And White DFS+剪枝
Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...