array / matrix subarray/submatrix sum
Maximal Subarray Sum : O(n) scan-and-update dynamic programming, https://en.wikipedia.org/wiki/Maximum_subarray_problem, https://leetcode.com/problems/maximum-subarray
Maximal Submatrix Sum: given 2-D matrix, find the submatrix whose sum is largest
we can solve 1-D case in O(n), then for each possible (i, j), generate column[] = sum{columns[i..j]}, which can be done in O(n) time given it's accumulating, and then solve 1-D case in O(n).
in total, all possible (i, j) means O(n^2), prefix-sum column means O(n), solve 1-D case O(n), in total O(n^3)
Shortest Subarray Sum Equals K : prefix-sum + sort + hash-table: O(nlogn) time, O(n) storage; https://leetcode.com/problems/minimum-size-subarray-sum/
if it's positive only, sliding window can also work.
Longest Subarray Sum Equals K : prefix-sum + sort + hash-table: O(n) time, O(n) storage, https://leetcode.com/problems/maximum-size-subarray-sum-equals-k
class Solution {
public:
int maxSubArrayLen(vector<int>& nums, int k) {
int sum = , res = ;
unordered_map<int, int> m;
for (int i = ; i < nums.size(); ++i) {
sum += nums[i];
if (sum == k) res = i + ;
else if (m.count(sum - k)) res = max(res, i - m[sum - k]);
if (!m.count(sum)) m[sum] = i;
}
return res;
}
};
Largest Subarray Sum A Less than K : change hash-table to a map, and lower_bound / upper_bound : prefix-sum + sort + BST : O(nlogn) time, O(n) storage; https://www.quora.com/Given-an-array-of-integers-A-and-an-integer-k-find-a-subarray-that-contains-the-largest-sum-subject-to-a-constraint-that-the-sum-is-less-than-k
int best_cumulative_sum(int ar[],int N,int K)
{
set<int> cumset;
cumset.insert();
int best=,cum=;
for(int i=;i<N;i++)
{
cum+=ar[i];
set<int>::iterator sit=cumset.upper_bound(cum-K);
if(sit!=cumset.end())best=max(best,cum-*sit);
cumset.insert(cum);
}
return best;
}
Rectangle: sum all possible [i, j] columns, and reduce the case to 1-D, in total O(n^2) * 1-D case.
Max Sum of Rectangle No Larger Than K : sum all possible [i, j] columns together, reduce to 1-D case, in total n^2*nlogn. https://leetcode.com/problems/max-sum-of-sub-matrix-no-larger-than-k/
array / matrix subarray/submatrix sum的更多相关文章
- Submatrix Sum
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...
- lintcode 中等题:Submatrix sum is 0 和为零的子矩阵
和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...
- [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- LeetCode862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- 862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- numpy中list array matrix比较
用python中的numpy包的时候不小心踩了array和matrix的大坑,又引申一下比较list array matrix之间的异同.数据结构(Data Structures)基本上人如其名——它 ...
- [LeetCode] 862. Shortest Subarray with Sum at Least K 和至少为K的最短子数组
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- array, matrix, list and dataframe
总结一下"入门3R"(Reading, 'Riting, 'Rrithmetic)中的读和写,不同的数据结构下的读写还是有点区别的. vector 命名 12 month.days ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
随机推荐
- hive job oom问题
错误信息例如以下:Container [pid=26845,containerID=container_1419056923480_0212_02_000001] is running beyond ...
- PHP购物车模块的实现(php/ajax/session)
购物车网页代码 1.登录界面login.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- PHP部分--图片上传服务器、图片路径存入数据库,并读取
html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- OIer同样是音乐家
烦闷的时候,shenben为大家准备了2首歌(不用耳机也能听哦) 只需把代码复制到dev-c++的编辑器上,轻按F11,然后聆听OIer的音乐…… 千本樱 曲谱 #include <cstdio ...
- 【BZOJ2654】tree 二分+最小生成树
[BZOJ2654]tree Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need ...
- sed相关
1 global flag sed 's/xxx/xxx/' inputfile,如果没有带global flag g的话,匹配替换的只是inputfile中的每一行的第一个匹配项.如果带了g的话,才 ...
- cordova 插件创建
peng@PENG-PC /E/_My_File_____/_work/MyCode/myCode/cordova-workspace/plugman-test/ABCD $ npm install ...
- 单机部署tomcat的shell脚本
单机部署tomcat的shell脚本,来自网络,自己需要时要根据自己的需求改动. #!/bin/sh # ############################################### ...
- requests不加代理
requests里的proxies不加代理可以设置为空,就会使用本机IP proxies={}
- GIT笔记:将项目发布到码云
GIT笔记:将项目发布到码云 发布步骤 1.码云创建项目 记录下项目的远程地址: https://gitee.com/mrsaber/ms_supplyAndSale.git 2.在本地创建GIT仓库 ...