leetCode_Max Sum of Rectangle No Larger Than K
来源:互联网 发布:好易网络电视下載 编辑:程序博客网 时间:2024/06/05 19:34
题意:给定一个二维整数数组,找出一个子矩形,使得该子矩形的和不超过K且和最大
思路:简单的思路就是枚举起点和终点,然后每行每列sum数组,加加减减即可,显然会超时。
优化的思路:枚举起点列和终点列,枚举每一行,把前面的和放在集合里,集合有lower_bound方法可以二分查找,找到合适的值即可。
代码如下:
class Solution {public: int maxSumSubmatrix(vector<vector<int>>& matrix, int r) { int i,j,k,m,n,ans=INT_MIN,ei,ej,temp; m=matrix.size(); if(m==0) return 0; else n=matrix[0].size(); for(i=0;i<n;i++) { vector<int> tsum(m,0); for(j=i;j<n;j++) { set<int> st{0}; int curSum=0; for(k=0;k<m;k++) { tsum[k]+=matrix[k][j]; curSum+=tsum[k]; auto it=st.lower_bound(curSum-r); if((it!=st.end())&&curSum-*it<=r) ans=max(ans,curSum-*it); st.insert(curSum); } } } return ans; }};
0 0
- leetCode_Max Sum of Rectangle No Larger Than K
- Leetcode Max Sum of Rectangle No Larger Than K
- [leetcode] 363. Max Sum of Rectangle No Larger Than K
- leetcode.363. Max Sum of Rectangle No Larger Than K
- 363. Max Sum of Rectangle No Larger Than K
- Max Sum of Rectangle No Larger Than K
- [LeetCode]--363. Max Sum of Rectangle No Larger Than K
- leetcode-363. Max Sum of Rectangle No Larger Than K
- [LeetCode]Max Sum of Rectangle No Larger Than K
- Max Sum of Rectangle No Larger Than K
- 363. Max Sum of Rectangle No Larger Than K[hard]
- 363. Max Sum of Rectangle No Larger Than K
- leetcode(363):Max Sum of Rectangle No Larger Than K
- [LeetCode] Max Sum of Rectangle No Larger Than K
- 363. Max Sum of Rectangle No Larger Than K
- Leetcode 363. Max Sum of Rectangle No Larger Than K
- 【Leetcode】363. Max Sum of Rectangle No Larger Than K
- 【Leetcode】Max Sum of Rectangle No Larger Than K
- 欢迎使用CSDN-markdown编辑器
- Retrofit源码解析---执行请求(DefaultCallAdapterFactory)
- 当 IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值
- LeetCode 62. Unique Paths
- UE4项目运行时显示鼠标指针
- leetCode_Max Sum of Rectangle No Larger Than K
- 用动态规划来优化0-1背包问题
- Retrofit源码解析---执行请求(RxJavaCallAdapterFactory)
- tomcat查看GC信息
- 教你如何免费升级XMind 8!
- LuaFramework-打包AssetBundle工具
- 递归获取目录下的所有文件
- eclipse导入项目时的常见错误
- 撸一段 SQL ? 还是撸一段代码?