[LeetCode] Maximum Subarray Sum
Dynamic Programming
There is a nice introduction to the DP algorithm in this Wikipedia article. The idea is to maintain a running maximum smax and a current summation sum. When we visit each num in nums, addnum to sum, then update smax if necessary or reset sum to 0 if it becomes negative.
The code is as follows.
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sum = , smax = INT_MIN;
for (int num : nums) {
sum += num;
if (sum > smax) smax = sum;
if (sum < ) sum = ;
}
return smax;
}
};
Divide and Conquer
The DC algorithm breaks nums into two halves and find the maximum subarray sum in them recursively. Well, the most tricky part is to handle the case that the maximum subarray may span the two halves. For this case, we use a linear algorithm: starting from the middle element and move to both ends (left and right ends), record the maximum sum we have seen. In this case, the maximum sum is finally equal to the middle element plus the maximum sum of moving leftwards and the maximum sum of moving rightwards.
Well, the code is just a translation of the above idea.
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int smax = INT_MIN, n = nums.size();
return maxSub(nums, , n - , smax);
}
private:
int maxSub(vector<int>& nums, int l, int r, int smax) {
if (l > r) return INT_MIN;
int m = l + ((r - l) >> );
int lm = maxSub(nums, l, m - , smax); // left half
int rm = maxSub(nums, m + , r, smax); // right half
int i, sum, ml = , mr = ;
// Move leftwards
for (i = m - , sum = ; i >= l; i--) {
sum += nums[i];
ml = max(sum, ml);
}
// Move rightwards
for (i = m + , sum = ; i <= r; i++) {
sum += nums[i];
mr = max(sum, mr);
}
return max(smax, max(ml + mr + nums[m], max(lm, rm)));
}
};
[LeetCode] Maximum Subarray Sum的更多相关文章
- Maximum Subarray Sum
Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...
- leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- [LeetCode]Maximum Subarray题解
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
- LeetCode Continuous Subarray Sum
原题链接在这里:https://leetcode.com/problems/continuous-subarray-sum/description/ 题目: Given a list of non-n ...
- [LeetCode] 560. Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- [leetcode]560. Subarray Sum Equals K 和为K的子数组
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
随机推荐
- node.js 操作excel 表格与XML文件常用的npm
在日常工作中会经常用到把一些excel表格文件转化为json,xml,js等格式的文件,下面就是我在日常中用到的这些npm. 1.node-xlsx: node-xlsx可以把excel文件转化为上面 ...
- Memcached真的过时了吗?
Memcached真的过时了吗? 这两年Redis火得可以,Redis也常常被当作Memcached的挑战者被提到桌面上来.关于Redis与Memcached的比较更是比比皆是.然而,Redis真的在 ...
- [Jobdu] 题目1373:整数中1出现的次数(从1到n整数中1出现的次数)
题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直困扰着他,特此他向JOBDU发来求助信,希望亲们能帮帮他.问题是:求出1~13的整数中1出现的次数,并算出100~130 ...
- Java运行Python脚本的几种方式
由于在项目需要执行Python,找寻相关资料,总结出以下几种方式: 直接执行Python脚本代码 引用 org.python包 PythonInterpreter interpreter = new ...
- CYQ学习主要摘要4
http://www.cnblogs.com/cyq1162/archive/2010/11/03/1867642.html Xml的处理 http://www.cnblogs.com/cyq1162 ...
- 严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.Invoc
错误提示:严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationTa ...
- 04、数据绑定控件 ListBox 的一个 Bug
同事这两天在做 universal 项目的时候,遇到一个诡异的问题,即使设置 Page 为 缓存状态, 在页面跳转后, ListBox 的位置不会被缓存,怀疑是页面的缓存状态出了问题: this.Na ...
- css 优先级的bug
对于前端而言,了解css样式的优先级,对开发或处理bug有着事半功倍的效果,今天在做项目的时候,突然碰到一个优先级的小问题,刚开始不知道所因,后来才发现这个问题是由优先级造成的.先描述下问题,鼠标悬停 ...
- 代码审计之Finecms任意文件下载漏洞
PS:该漏洞已被公布,只是学习.故自己跟着大佬的步伐审计. 文件地址:\controllers\ApiController.php Line 57 public function downAction ...
- eclipse不能自动编译生成class文件的解决办法
最近在项目项目开发过程中遇到eclipse不能自动编译生成class文件,当时很纳闷,每次修改代码后运行都是修改前的效果,没辙了,只好反编译原来的class文件,结果发现,class文件里并没有看到修 ...