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 ...
随机推荐
- python 基础 8.5 re 的match对象
#/usr/bin/python #coding=utf-8 #@Time :2017/11/18 21:49 #@Auther :liuzhenchuan #@File :match对象.p ...
- FPGA学习笔记之Altera FPGA使用JIC文件配置固化教程(转)
很多做过单片机的朋友都知 道,我们在对MCU烧写完程序固件后,那么该程序固件就存储在了该MCU内部.即使MCU断电了再重新上电,程序也能继续运行.这是因为对MCU烧写固件 的实质就是将程序固件写入到M ...
- Node.js面试题
Node.js面试题列表 什么是错误优先的回调函数? 如何避免回调地狱? 如何用Node来监听80端口? 什么是事件循环? 哪些工具可以用来保证一致的编程风格? 什么是测试金字塔?对于HTTP API ...
- Double Check Locking 双检查锁机制
方法保证了多线程并发下的线程安全性.这里在声明变量时使用了volatile关键字来保证其线程间的可见性:在同步代码块中使用二次检查,以保证其不被重复实例化.集合其二者,这种实现方式既保证了其高效性,也 ...
- N皇后问题算法
N皇后问题的两种主要算法是试探回溯法和位运算法.前一种是经典算法,后一种是目前公认的最高效算法,后者比前者效率提高了至少一个数量级.很多问题可以借鉴位运算的思想. 以下是转载的我认为写的比较好的一篇N ...
- mysql系列之3.mysql进阶
启动原理 mysqld脚本-->mysqld_safe脚本-->mysqld服务-->启动mysql 强制关闭mysql: 三种方法, 不建议用! killall mysqld pk ...
- 解决Ubuntu(乌班图)vi/vim模式下粘贴的代码内容会多出的空格的问题
vi/vim模式下的粘贴 因为linux系统和win系统的差异性,有时候在win环境运行的python代码会放在Linux系统上执行,这个时候就需要把win系统上IDE上的代码copy下来,在Linu ...
- 流畅的python学习笔记:第九章:符合python风格的对象
首先来看下对象的表现形式: class People(): def __init__(self,name,age): self.name=name self.a ...
- sed 简单用法
sed的一个简单用法: eg:在某一个文件中的一个aaa字段前后添加某些字段. 在aaa前面添加字段:sed -ne 's/aaa/&HELLO/p' test 输出结果:aaaHELLO 在 ...
- mysql的安装与基本管理
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS ...