[leetcode] 363. Max Sum of Rectangle No Larger Than K
来源:互联网 发布:王权富贵东方月初乐乎 编辑:程序博客网 时间:2024/05/17 06:38
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.
Example:
Given matrix = [ [1, 0, 1], [0, -2, 3]]k = 2
The answer is 2
. Because the sum of rectangle [[0, 1], [-2, 3]]
is 2 and 2 is the max number no larger than k (k = 2).
Note:
- The rectangle inside the matrix must have an area > 0.
- What if the number of rows is much larger than the number of columns?
这道题是找二维数组中相加和最大且不大于K的矩形,题目难度为Hard。
乍一看题目蛮复杂的,如果把题目从二维降到一维该如何处理呢?一维数组中处理起来相对简单很多,遍历一维数组,用curSum[j]表示位置j之前所有数组元素之和,依次将curSum存入set中(用set存储便于后续二分查找),遍历到位置j时,如果j之前存在位置i满足curSum[j] - curSum[i] <= K,表明以位置j结尾的序列有满足条件不大于K的,在set中二分查找curSum[j] - K,查找到的位置即是以j结尾且相加和不大于K的最大序列的开始位置,最后比较并更新最大序列和;如果不存在满足curSum[j] - curSum[i] <= K的位置i,表明以位置j结尾的所有序列相加和均大于K。遍历整个一维数组即可得到最终结果。具体代码如下:
int maxSumSeq(vector<int>& num, int k) { if(num.empty()) return 0; int ret = INT_MIN, curSum = 0; set<int> sum; sum.insert(0); for(int i=0; i<num.size(); ++i) { curSum += num[i]; auto it = sum.lower_bound(curSum-k); if(it != sum.end()) ret = max(ret, curSum-*it); sum.insert(curSum); } return ret;}
在set中插入0是为了方便二分查找,同时表示下标0之前相加和是0。
一维数组问题解决了,如何扩展到二维数组呢?通过双层遍历行或列即可得到行或列所有可能的区间,例如选择列来进行遍历,针对每个列区间计算该区间中每行区间内元素之和,将列区间内每行元素作为一个整体,这样就把二维数组问题转化为一维数组了,再按照上面处理一维数组的方法即可得到最终结果。具体代码:
class Solution {public: int maxSumSubmatrix(vector<vector<int>>& matrix, int k) { int row = matrix.size(); if(!row) return 0; int col = matrix[0].size(); if(!col) return 0; int ret = INT_MIN; for(int i=0; i<col; ++i) { vector<int> sum(row, 0); for(int j=i; j<col; ++j) { for(int r=0; r<row; ++r) sum[r] += matrix[r][j]; int curSum = 0, curMax = INT_MIN; set<int> sumSet; sumSet.insert(0); for(int r=0; r<row; ++r) { curSum += sum[r]; auto it = sumSet.lower_bound(curSum-k); if(it != sumSet.end()) curMax = max(curMax, curSum-*it); sumSet.insert(curSum); } ret = max(ret, curMax); } } return ret; }};题目追问行比列大很多的情况如何优化?假如矩阵为m行n列,则上面代码的时间复杂度为O(n^2*mlogm),如果外层循环选择行进行遍历,则复杂度变为O(m^2*nlogn),比列遍历大很多,所以在代码中外层循环选择列进行遍历。
- [leetcode] 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-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]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
- [LeetCode]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
- 【Leetcode】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[hard]
- 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
- xml格式数据解析
- 社会化分享在QQ互联后台的URL schema应该怎么设置?
- android中实现文字的超链接的应用内跳转
- HDU2502 月之数(解法三)
- BaaS云架构核心模式之Serverless架构 - 用服务代替服务器(Martin Fowler)
- [leetcode] 363. Max Sum of Rectangle No Larger Than K
- Linux 2.2.0 中select()的主要代码片断——The C10M Problem
- nginx安装
- 时钟dome,css3
- Sql优化方法
- SDKD 2016 Summer Single Contest #01.D
- JDBC中prepareStatement 与createStatement 区别
- Hibernate的抓取策略
- ReactiveCocoa 用 RACSignal 替代 Delegate