题目链接

链接:https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/description/

题解&代码

1、暴力枚举所有的情况,时间复杂度O(n2*m2),实际耗时759 ms

  1. class Solution {
  2. public:
  3. int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
  4. int n=matrix.size(),m=matrix[0].size();
  5. vector<vector<int> > sumv(n+1,vector<int>(m+1,0));
  6. for(int i=1; i<=n; i++) {
  7. for(int j=1; j<=m; j++) {
  8. sumv[i][j]=matrix[i-1][j-1]+sumv[i-1][j]+sumv[i][j-1]-sumv[i-1][j-1];
  9. }
  10. }
  11. int ans=-INF;
  12. for(int i1=1;i1<=n;i1++){
  13. for(int j1=1;j1<=m;j1++){
  14. for(int i2=i1;i2<=n;i2++){
  15. for(int j2=j1;j2<=m;j2++){
  16. int val=sumv[i2][j2]-sumv[i1-1][j2]-sumv[i2][j1-1]+sumv[i1-1][j1-1];
  17. if(val<=k){
  18. ans=max(ans,val);
  19. }
  20. }
  21. }
  22. }
  23. }
  24. return ans;
  25. }
  26. private:
  27. static const int INF=0x3f3f3f3f;
  28. };

2、先通过枚举一维进行降维,然后再暴力枚举所有区间,时间复杂度O(m2*n2),实际耗时736ms。

  1. class Solution {
  2. public:
  3. int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
  4. int n=matrix.size(),m=matrix[0].size();
  5. vector<vector<int> > sumv(n+1,vector<int>(m+1,0));
  6. for(int i=1; i<=n; i++) {
  7. for(int j=1; j<=m; j++) {
  8. sumv[i][j]=sumv[i][j-1]+matrix[i-1][j-1];
  9. }
  10. }
  11. int ans=-INF;
  12. vector<int> arr(n+1);
  13. for(int len=1; len<=m; len++) {
  14. for(int j=len; j<=m; j++) {
  15. arr[0]=0;
  16. for(int i=1; i<=n; i++) {
  17. int val=sumv[i][j]-sumv[i][j-len];
  18. arr[i]=val+arr[i-1];
  19. }
  20. for(int i=1;i<=n;i++){
  21. for(int j=0;j<i;j++){
  22. int x=arr[i]-arr[j];
  23. if(x<=k) ans=max(ans,x);
  24. }
  25. }
  26. }
  27. }
  28. return ans;
  29. }
  30. private:
  31. static const int INF=0x3f3f3f3f;
  32. };

3、 第2种方法基础上加一个剪枝,在枚举区间之前,先用dp(O(n))求出最大值区间和最小值区间,然后判断k是否在区间内,时间复杂度:‘玄学’,实际耗时19ms。

  1. class Solution {
  2. public:
  3. int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
  4. int n=matrix.size(),m=matrix[0].size();
  5. vector<vector<int> > sumv(n+1,vector<int>(m+1,0));
  6. for(int i=1; i<=n; i++) {
  7. for(int j=1; j<=m; j++) {
  8. sumv[i][j]=sumv[i][j-1]+matrix[i-1][j-1];
  9. }
  10. }
  11. int ans=-inf;
  12. vector<int> arr(n+1);
  13. for(int len=1; len<=m; len++) {
  14. for(int j=len; j<=m; j++) {
  15. arr[0]=0;
  16. LL mi=inf,ma=-inf;
  17. LL pre=inf,aft=-inf;
  18. for(int i=1; i<=n; i++) {
  19. int val=sumv[i][j]-sumv[i][j-len];
  20. arr[i]=val+arr[i-1];
  21. pre=min(pre+val,(LL)val);
  22. aft=max(aft+val,(LL)val);
  23. mi=min(mi,pre);
  24. ma=max(ma,aft);
  25. }
  26. if(k<mi) continue;
  27. else if(k==mi) {
  28. ans=k;
  29. } else if(k>=ma) {
  30. ans=max((LL)ans,ma);
  31. } else {
  32. for(int i=1; i<=n; i++) {
  33. for(int j=0; j<i; j++) {
  34. int x=arr[i]-arr[j];
  35. if(x<=k) ans=max(ans,x);
  36. }
  37. }
  38. if(ans==k) return ans;
  39. }
  40. }
  41. }
  42. return ans;
  43. }
  44. private:
  45. static const int inf=0x7fffffff;
  46. typedef long long LL;
  47. };

4、正解:先降维,然后枚举区间终点,用set维护前缀和,用lower_bound去查区间起点,从而O(nlogn)的复杂度解决该问题的一维版本。加上降维的时间复杂度为O(m^2*nlogn)。实际耗时:259ms。

  1. class Solution {
  2. public:
  3. int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
  4. int n=matrix.size(),m=matrix[0].size();
  5. vector<vector<int> > sumv(n+1,vector<int>(m+1,0));
  6. for(int i=1; i<=n; i++) {
  7. for(int j=1; j<=m; j++) {
  8. sumv[i][j]=sumv[i][j-1]+matrix[i-1][j-1];
  9. }
  10. }
  11. int ans=-INF;
  12. vector<int> pre(n+1),arr(n+1);
  13. set<pair<int,int> > myset;
  14. set<pair<int,int> >::iterator it;
  15. for(int len=1; len<=m; len++) {
  16. for(int j=len; j<=m; j++) {
  17. pre[0]=0;
  18. myset.clear();
  19. for(int i=1; i<=n; i++) {
  20. arr[i]=sumv[i][j]-sumv[i][j-len];
  21. pre[i]=arr[i]+pre[i-1];
  22. myset.insert(make_pair(pre[i],i));
  23. }
  24. for(int i=1;i<=n;i++){
  25. if(arr[i]<=k) ans=max(ans,arr[i]);
  26. int key=k-arr[i]+pre[i];
  27. it=myset.lower_bound(make_pair(key,0x7fffffff));
  28. if(it!=myset.begin()){
  29. it--;
  30. int tmp=it->first;
  31. ans=max(ans,tmp-pre[i]+arr[i]);
  32. }
  33. myset.erase(make_pair(pre[i],i));
  34. }
  35. }
  36. }
  37. return ans;
  38. }
  39. private:
  40. static const int INF=0x7fffffff;
  41. };

5、正解优化版本(空间优化+常数优化)实际耗时:162ms。

  1. class Solution {
  2. public:
  3. int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
  4. int n=matrix.size(),m=matrix[0].size();
  5. int mi=min(n,m),ma=max(n,m);
  6. bool isColMax=m>n;
  7. int ans=-2147483648;
  8. for(int st=1; st<=mi; st++) {
  9. vector<int> arr(ma+1,0);
  10. for(int ed=st; ed<=mi; ed++) {
  11. set<int> myset;
  12. int cnt=0;
  13. myset.insert(cnt);
  14. for(int i=1; i<=ma; i++) {
  15. arr[i]+=isColMax?matrix[ed-1][i-1]:matrix[i-1][ed-1];
  16. cnt+=arr[i];
  17. set<int>::iterator it=myset.lower_bound(cnt-k);
  18. if(it!=myset.end()) ans=max(ans,cnt-*it);
  19. myset.insert(cnt);
  20. }
  21. }
  22. }
  23. return ans;
  24. }
  25. };

6、方法5+方法3提到的dp剪枝:实际耗时:19ms。

  1. class Solution {
  2. public:
  3. int maxSumSubmatrix(vector<vector<int> >& matrix, int k) {
  4. int n=matrix.size(),m=matrix[0].size();
  5. int mi=min(n,m),ma=max(n,m);
  6. bool isColMax=m>n;
  7. int ans=-2147483648;
  8. for(int st=1; st<=mi; st++) {
  9. vector<int> arr(ma+1,0);
  10. for(int ed=st; ed<=mi; ed++) {
  11. set<int> myset;
  12. int cnt=0;
  13. myset.insert(cnt);
  14. for(int i=1; i<=ma; i++) {
  15. arr[i]+=isColMax?matrix[ed-1][i-1]:matrix[i-1][ed-1];
  16. cnt+=arr[i];
  17. set<int>::iterator it=myset.lower_bound(cnt-k);
  18. if(it!=myset.end()) ans=max(ans,cnt-*it);
  19. myset.insert(cnt);
  20. }
  21. }
  22. }
  23. return ans;
  24. }
  25. };

LeetCode 363:Max Sum of Rectangle No Larger Than K的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...

  4. 【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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. Java输出打印工具类封装

    在进行Java打印输出,进行查看字段值的时候,觉得每次写了System.out.println之后,正式发布的时候,还得一个个的删掉,太麻烦了,经过别人的指教,做了一个Java的打印输出封装类,只为记 ...

  2. CSS命名方式=》BEM

    时间:2016-11-04 20:04:53 原文地址:https://github.com/zhongxia245/blog/issues/48 一.背景 挺早就听说过BEM了,也大概的知道怎么用, ...

  3. 17秋 软件工程 团队第五次作业 Alpha Scrum7

    17秋 软件工程 团队第五次作业 Alpha Scrum7 今日完成的任务 世强:部员详情列表的编写与数据交互,完善APP通知模块: 港晨:完成前端登陆界面编写: 树民:完善Web后端数据库访问模块: ...

  4. 17秋 软件工程 Alpha展示博客

    成员简介 姓名 个人简介 博客地址 郑世强 郑世强,计算机三班,了解java web端和Android端编程,使用过Spring MVC和Spring Boot开发商业程序,Android端学习了rx ...

  5. PyQt5 + QtDesigner

    看到网上蛮多介绍做界面开发时可以借助QtDesigner进行快速完成布局,搞了半天在电脑里却找不到该工具,网上查了一下,原来是要额外安装一个pyqt5的工具包,下面结合亲身一步一步操作记录下来,也方便 ...

  6. 【 nginx 】怎么安装nginx

    一,下载地址:http://nginx.org/en/download.html 二,下载完成之后,是一个安装包,解压之后就能直接使用 三,点击进去我们刚刚解压好的nginx的安装包,打开nginx. ...

  7. Java SE和Java EE应用的性能调优

    凡事预则立,不预则废,和很多事情一样.Java性能调优的成功.离不开行动计划.方法或策略以及特定的领域背景知识.为了在Java性能调优工作中有所成就.你得超越"花似雾中看"的状态, ...

  8. centos7下安装docker(9.1容器对资源的使用限制-CPU)

    默认情况下,所有容器可以平等的使用host上的CPU资源并没有限制 1.docker可以通过-c或者--cpu-shares设置容器使用的权重.如果不指定,默认值为1024. 与内存的限额不同,通过- ...

  9. 2018-2019-2 网络对抗技术 20165318 Exp5 MSF基础应用

    2018-2019-2 网络对抗技术 20165318 Exp5 MSF基础应用 原理与实践说明 实践原理 实践内容概述 基础问题回答 攻击实例 主动攻击的实践 ms08_067_netapi:自动化 ...

  10. logstash同步mysql数据到mysql(问题一)

    问题 通过logstash同步数据时 字段类型为tinyint时 通过过去 0变成了false  1变为了true 时间类型 变为 2018-10-16T14:58:02.871Z 分析 开始尝试通过 ...