第十三周 Leetcode 363. Max Sum of Rectangle No Larger Than K(HARD)
思路: 一种naive的算法就是枚举每个矩形块, 时间复杂度为O((mn)^2), 可以做少许优化时间复杂度可以降低到O(mnnlogm), 其中m为行数, n为列数.
先求出任意两列之间的所有数的和, 然后再枚举任意两行之间的和, 而我们优化的地方就在后者. 我们用s[x]来表示第x行从a列到b列的和. 遍历一遍从第0行到最后一行的求和数组, 并依次将其放到二叉搜索树中, 这样当我们知道了从第0行到当前行的和的值之后, 我们就可以用lower_bound在O(log n)的时间复杂度内找到能够使得从之前某行到当前行的矩阵值最接近k. 也就是说求在之前的求和数组中找到第一个位置使得大于(curSum - k), 这种做法的原理是在curSum之下规定了一个bottom-line, 在这上面的第一个和就是(curSum-val)差值与k最接近的数. 还需要注意的是预先为二叉搜索树加一个0值, 这种做法的原理是如果当前curSum小于k, 那么至少本身是一个潜在的解. 说的有点乱, 请见谅, 不懂的请提问!
class Solution {
public:
int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {
if(matrix.size()==0) return 0;
int row = matrix.size(), col = matrix[0].size();
int ans =INT_MIN;
for(int i = 0; i < col; i++)
{
vector<int> sum(row, 0);
for(int j =i; j < col; j++)
{
set<int> st{0};
int curSum =0, curMax = INT_MIN;
for(int x = 0; x < row; x++)
{
sum[x] += matrix[x][j];
curSum += sum[x];
auto it = st.lower_bound(curSum-k);
if(it!=st.end()) curMax = max(curSum - *it, curMax);
st.insert(curSum);
}
ans = max(curMax, ans);
}
}
return ans;
}
};
第十三周 Leetcode 363. Max Sum of Rectangle No Larger Than K(HARD)的更多相关文章
- [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 ...
- 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 解题报告(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 ...
- 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] 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 ...
- [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 ...
随机推荐
- 【C++】使用find函数快速定位元素
当有了STL,你还在用遍历的土方法定位元素吗? 今天就来介绍一下,如何使用algorithm库里的find函数快速定位数组或向量中的元素. 首先当然要包含头文件: #include <algor ...
- LeetCode(47)Permutations II
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...
- ECNU 3260 袋鼠妈妈找孩子(dfs)
链接:http://acm.ecnu.edu.cn/problem/3260/ 题意: 给出一个x,y,k.求从左上角到(x,y)最短路径不少于k而且最快到达(x,y)的迷宫.(迷宫有多个 输出其中一 ...
- git clone问题
中秋节回来上班 竟然忘记带电脑了  ̄□ ̄||还好同事有备用电脑,这要是回去拿估计上午都不用干什么了,用同事电脑当然需要安装环境,下面说一下git上遇到的问题吧 (1)首先我尝试用https方式克隆代码 ...
- Vue页面骨架屏(一)
在开发webapp的时候总是会受到首屏加载时间过长的影响,主流的解决方法是在载入完成之前显示loading图效果,而一些大公司会配置一套服务端渲染的架构来解决这个问题.考虑到ssr所要解决的一系列问题 ...
- W3C Blog: HTML-下一步是什么?
HTML5.1 草稿版: 地址:https://www.w3.org/TR/html51/introduction.html#a-quick-introduction-to-html 2016年3月0 ...
- [luoguP2885] [USACO07NOV]电话线Telephone Wire(DP + 贪心)
传送门 真是诡异. 首先 O(n * 100 * 100) 三重循环 f[i][j] 表示到第 i 个柱子,高度是 j 的最小花费 f[i][j] = min(f[i - 1][k] + abs(k ...
- hdu 3691最小割将一个图分成两部分
转载地址:http://blog.csdn.net/xdu_truth/article/details/8104721 题意:题给出一个无向图和一个源点,让你求从这个点出发到某个点最大流的最小值.由最 ...
- C++字符串读入
int read() { ,f=;char ch=getchar(); ;ch=getchar();} +ch-';ch=getchar();} return x*f; } int main() { ...
- POJ 2828 Buy Tickets (线段树 || 树状数组)
题目大意 一些小朋友在排队,每次来一个人,第i个人会插到第x个人的后面.权值为y.保证x∈[0,i-1]. 按照最后的队伍顺序,依次输出每个人的权值. 解题分析 好气吖.本来是在做splay练习,然后 ...