LeetCode 363:Max Sum of Rectangle No Larger Than K
题目链接
链接:https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/description/
题解&代码
1、暴力枚举所有的情况,时间复杂度O(n2*m2),实际耗时759 ms
class Solution {
public:
int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
int n=matrix.size(),m=matrix[0].size();
vector<vector<int> > sumv(n+1,vector<int>(m+1,0));
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
sumv[i][j]=matrix[i-1][j-1]+sumv[i-1][j]+sumv[i][j-1]-sumv[i-1][j-1];
}
}
int ans=-INF;
for(int i1=1;i1<=n;i1++){
for(int j1=1;j1<=m;j1++){
for(int i2=i1;i2<=n;i2++){
for(int j2=j1;j2<=m;j2++){
int val=sumv[i2][j2]-sumv[i1-1][j2]-sumv[i2][j1-1]+sumv[i1-1][j1-1];
if(val<=k){
ans=max(ans,val);
}
}
}
}
}
return ans;
}
private:
static const int INF=0x3f3f3f3f;
};
2、先通过枚举一维进行降维,然后再暴力枚举所有区间,时间复杂度O(m2*n2),实际耗时736ms。
class Solution {
public:
int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
int n=matrix.size(),m=matrix[0].size();
vector<vector<int> > sumv(n+1,vector<int>(m+1,0));
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
sumv[i][j]=sumv[i][j-1]+matrix[i-1][j-1];
}
}
int ans=-INF;
vector<int> arr(n+1);
for(int len=1; len<=m; len++) {
for(int j=len; j<=m; j++) {
arr[0]=0;
for(int i=1; i<=n; i++) {
int val=sumv[i][j]-sumv[i][j-len];
arr[i]=val+arr[i-1];
}
for(int i=1;i<=n;i++){
for(int j=0;j<i;j++){
int x=arr[i]-arr[j];
if(x<=k) ans=max(ans,x);
}
}
}
}
return ans;
}
private:
static const int INF=0x3f3f3f3f;
};
3、 第2种方法基础上加一个剪枝,在枚举区间之前,先用dp(O(n))求出最大值区间和最小值区间,然后判断k是否在区间内,时间复杂度:‘玄学’,实际耗时19ms。
class Solution {
public:
int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
int n=matrix.size(),m=matrix[0].size();
vector<vector<int> > sumv(n+1,vector<int>(m+1,0));
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
sumv[i][j]=sumv[i][j-1]+matrix[i-1][j-1];
}
}
int ans=-inf;
vector<int> arr(n+1);
for(int len=1; len<=m; len++) {
for(int j=len; j<=m; j++) {
arr[0]=0;
LL mi=inf,ma=-inf;
LL pre=inf,aft=-inf;
for(int i=1; i<=n; i++) {
int val=sumv[i][j]-sumv[i][j-len];
arr[i]=val+arr[i-1];
pre=min(pre+val,(LL)val);
aft=max(aft+val,(LL)val);
mi=min(mi,pre);
ma=max(ma,aft);
}
if(k<mi) continue;
else if(k==mi) {
ans=k;
} else if(k>=ma) {
ans=max((LL)ans,ma);
} else {
for(int i=1; i<=n; i++) {
for(int j=0; j<i; j++) {
int x=arr[i]-arr[j];
if(x<=k) ans=max(ans,x);
}
}
if(ans==k) return ans;
}
}
}
return ans;
}
private:
static const int inf=0x7fffffff;
typedef long long LL;
};
4、正解:先降维,然后枚举区间终点,用set维护前缀和,用lower_bound去查区间起点,从而O(nlogn)的复杂度解决该问题的一维版本。加上降维的时间复杂度为O(m^2*nlogn)。实际耗时:259ms。
class Solution {
public:
int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
int n=matrix.size(),m=matrix[0].size();
vector<vector<int> > sumv(n+1,vector<int>(m+1,0));
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
sumv[i][j]=sumv[i][j-1]+matrix[i-1][j-1];
}
}
int ans=-INF;
vector<int> pre(n+1),arr(n+1);
set<pair<int,int> > myset;
set<pair<int,int> >::iterator it;
for(int len=1; len<=m; len++) {
for(int j=len; j<=m; j++) {
pre[0]=0;
myset.clear();
for(int i=1; i<=n; i++) {
arr[i]=sumv[i][j]-sumv[i][j-len];
pre[i]=arr[i]+pre[i-1];
myset.insert(make_pair(pre[i],i));
}
for(int i=1;i<=n;i++){
if(arr[i]<=k) ans=max(ans,arr[i]);
int key=k-arr[i]+pre[i];
it=myset.lower_bound(make_pair(key,0x7fffffff));
if(it!=myset.begin()){
it--;
int tmp=it->first;
ans=max(ans,tmp-pre[i]+arr[i]);
}
myset.erase(make_pair(pre[i],i));
}
}
}
return ans;
}
private:
static const int INF=0x7fffffff;
};
5、正解优化版本(空间优化+常数优化)实际耗时:162ms。
class Solution {
public:
int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
int n=matrix.size(),m=matrix[0].size();
int mi=min(n,m),ma=max(n,m);
bool isColMax=m>n;
int ans=-2147483648;
for(int st=1; st<=mi; st++) {
vector<int> arr(ma+1,0);
for(int ed=st; ed<=mi; ed++) {
set<int> myset;
int cnt=0;
myset.insert(cnt);
for(int i=1; i<=ma; i++) {
arr[i]+=isColMax?matrix[ed-1][i-1]:matrix[i-1][ed-1];
cnt+=arr[i];
set<int>::iterator it=myset.lower_bound(cnt-k);
if(it!=myset.end()) ans=max(ans,cnt-*it);
myset.insert(cnt);
}
}
}
return ans;
}
};
6、方法5+方法3提到的dp剪枝:实际耗时:19ms。
class Solution {
public:
int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
int n=matrix.size(),m=matrix[0].size();
int mi=min(n,m),ma=max(n,m);
bool isColMax=m>n;
int ans=-2147483648;
for(int st=1; st<=mi; st++) {
vector<int> arr(ma+1,0);
for(int ed=st; ed<=mi; ed++) {
set<int> myset;
int cnt=0;
myset.insert(cnt);
for(int i=1; i<=ma; i++) {
arr[i]+=isColMax?matrix[ed-1][i-1]:matrix[i-1][ed-1];
cnt+=arr[i];
set<int>::iterator it=myset.lower_bound(cnt-k);
if(it!=myset.end()) ans=max(ans,cnt-*it);
myset.insert(cnt);
}
}
}
return ans;
}
};
LeetCode 363:Max Sum of Rectangle No Larger Than 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...
- 【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] 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- [bug]android monkey命令在Android N和Android O上的一点差异发现
最近再调试这个统计FPS的代码,发现代码在android N上可以正常运行,但在android O上却运行不了,拼了命的报错,给出的提示就是 ZeroDivisionError: division b ...
- nginx与location语法详解
Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 ^~ 以某个字符串开头 ~ 区分大小写的正则匹配 ~* 不区分大小写的正则匹配 !~ 区分大小写不匹配的正则 !~* 不区分大小 ...
- HTTP 请求方法
一.HTTP中定义了以下几种请求方法: 1.GET:2.POST:3.PUT:4.DELETE; 5.HEAD:6.TRACE:7.OPTIONS: 二.各个方法介绍: 1.GET方法: 对这个资源的 ...
- UVA1103-Ancient Messages(脑洞+dfs)
Problem UVA1103-Ancient Messages Accept: 1176 Submit: 6103 Time Limit: 3000 mSec Problem Descriptio ...
- 【LGP5176】公约数
题目 老年选手啥都不想推只能搞个杜教筛了 这个式子长得好吓人啊,所以我们唯一分解之后来考虑这道题 设\(i,j,k\)分别是\(p^a,p^b,p^c\),至于到底谁是谁并不重要,我们不妨假设\(a\ ...
- 【转】MySQL理解索引、添加索引的原则
索引用于快速找出在某个列中有一特定值的行.不使用索引,MySQL必须从第1条记录开始然后读完整个表直到找出相关的行,还需要考虑每次读入数据页的IO开销.而如果采取索引,则可以根据索引指向的页以及记录在 ...
- 转载 【.NET基础】--委托、事件、线程(3)
之前的两篇文章我们了解了委托和事件,本文我们看一下线程. 1,一个窗体程序,默认拥有一个线程(相当于一个商店里面,只有一个店员),这个默认的线程叫做 UI线程/主线程. 2,进程和线程的关系: A ...
- maven 仓库配置 pom中repositories属性
文章转自http://blog.csdn.net/zlgydx/article/details/51130627 什么是Maven仓库在不用Maven的时候,比如说以前我们用Ant构建项目,在项目目录 ...
- ESP32 ADC
2个12位的ADC,共计18通道,ADC2比较特殊的一点就是:ADC2和wifi共用,wifi的优先级更高,所以ADC2只有在WIFI模块不用的情况下好使: 在读取ADC数据之前,必须先对ADC进行设 ...
- STM32固件库详解
STM32固件库详解 emouse原创文章,转载请注明出处http://www.cnblogs.com/emouse/ 应部分网友要求,最新加入固件库以及开发环境使用入门视频教程,同时提供例程模板 ...