Maximal Subarray Sum : O(n) scan-and-update dynamic programming, https://en.wikipedia.org/wiki/Maximum_subarray_problemhttps://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的更多相关文章

  1. Submatrix Sum

    Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...

  2. lintcode 中等题:Submatrix sum is 0 和为零的子矩阵

    和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...

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

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

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

  6. numpy中list array matrix比较

    用python中的numpy包的时候不小心踩了array和matrix的大坑,又引申一下比较list array matrix之间的异同.数据结构(Data Structures)基本上人如其名——它 ...

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

  8. array, matrix, list and dataframe

    总结一下"入门3R"(Reading, 'Riting, 'Rrithmetic)中的读和写,不同的数据结构下的读写还是有点区别的. vector 命名 12 month.days ...

  9. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

随机推荐

  1. WCF服务返回XML或JSON格式数据

    第一种方式public string GetData( string format) { string res = null; Student stu = new Student { StuID = ...

  2. CentOS6.5升级内核从2.6.32到3.2.14

    由于最近想要在服务器上跑IOU,但是在部署VMware后发现不能正常启动,总是提示内核无法载入,什么C header files matching your running kernel were n ...

  3. EasyPlayerPro Windows播放器电子放大/局部放大播放功能实现

    背景描述 在视频监控软件中,我们看到很多的软件都有电子放大功能, 按住鼠标左键不放,框选一个区域,再松开鼠标左键,即对选中的区域进行放大显示, 且可以重复该操作,逐步放大所需显示的区域, 有没有觉得, ...

  4. Go 语言中的数组是一种 值类型(不像 C/C++ 中是指向首元素的指针)

    the-way-to-go_ZH_CN/07.1.md at master · Unknwon/the-way-to-go_ZH_CN https://github.com/Unknwon/the-w ...

  5. Consumer Group Example

    面向kafka编程 Consumer Group Example https://cwiki.apache.org/confluence/display/KAFKA/Consumer+Group+Ex ...

  6. 【题解】CF359B Permutation

    [题解]CF359B Permutation 求一个长度为\(2n\)的序列,满足\(\Sigma |a_{2i}-a_{2i-1}|-|\Sigma a_{2i}-a_{2i-1}|=2k\) 这种 ...

  7. linux c编程:文件的读写

    Linux系统中提供了系统调用函数open()和close()用于打开和关闭一个存在的文件 int open(const char *pathname,int flags) int open(cons ...

  8. ABAP 关键字(1)

    1.定义DATA ,TYPES TYPES关键字用于创建自定义数据类型,就像JAVA里面创建类一样,用TYPES创建的数据类型可以被其它变量引用(类似于实例化对象),而本身不能直接引用或者赋值. DA ...

  9. java中的clone方法

    Java中对象的创建 clone顾名思义就是复制, 在Java语言中, clone方法被对象调用,所以会复制对象.所谓的复制对象,首先要分配一个和源对象同样大小的空间,在这个空间中创建一个新的对象.那 ...

  10. HBase存储方案设计

    需求描述 将数据记录持久化存储在HBase中,需要支持如下功能: 支持高吞吐量读写操作,实时采集10,000条/秒: 支持动态添加字段: 支持服务端过滤: 支持部分字段修改. 设计方案 按列存储 优点 ...