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的更多相关文章

  1. Maximum Subarray Sum

    Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...

  2. 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题是存储的当前累加和的 ...

  3. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  4. LeetCode: Maximum Subarray 解题报告

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  5. [LeetCode]Maximum Subarray题解

    Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...

  6. LeetCode Continuous Subarray Sum

    原题链接在这里:https://leetcode.com/problems/continuous-subarray-sum/description/ 题目: Given a list of non-n ...

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

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

  9. 【leetcode】1186. Maximum Subarray Sum with One Deletion

    题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...

随机推荐

  1. effactive java读书小结1

    java程序设计的原则 1 清晰性和原则性最为重要:模块:任何可重用的软件组件,从单个方法到复杂系统都可以是一个模块.代码应该被重用而不是被拷贝.模块之间的依赖性应该降到最小:错误应尽早检查出来,最好 ...

  2. 有关View的几个基础知识点-IOS开发

    转自:http://blog.csdn.net/iukey/article/details/7083165 我一般情况下不会使用interface builder去画界面,而是用纯代码去创建界面,不是 ...

  3. python学习笔记(7)--爬虫隐藏代理

    说明: 1. 好像是这个网站的代理http://www.xicidaili.com/ 2. 第2,3行的模块不用导入,之前的忘删了.. 3. http://www.whatismyip.com.tw/ ...

  4. 方程式漏洞之复现window2008/win7 远程命令执行漏洞

    前几天就想写的,因为一些缘故就没写.此次是在外网环境下进行的.大家在内网中也一个样. 方法: 使用Eternalblue模块,剑测是否有漏洞然后msf生成一个dll直接反弹shell. PS:win版 ...

  5. Unix系统编程()发送信号的其他方式:raise和killpg

    有时,进程需要向自身发送信号,raise 函数就执行了这一任务. #include <signal.h> int raise(int sig); 在单线程程序中,调用raise相当于对ki ...

  6. 基于Ambari构建自己的大数据平台产品

    目前市场上常见的企业级大数据平台型的产品主流的有两个,一个是Cloudera公司推出的CDH,一个是Hortonworks公司推出的一套HDP,其中HDP是以开源的Ambari作为一个管理监控工具,C ...

  7. 利用WordPress用户密码算法规则修改用户密码

    WordPress用户密码保存在wp_users数据表的user_pass字段,密码是通过Portable PHP password hashing framework类产生的, 密码的形式是随机且不 ...

  8. ffmpeg 从内存中读取数据 .

    http://blog.csdn.net/leixiaohua1020/article/details/12980423 ——————————————————————————————————————— ...

  9. 原生js怎么删除一个 div

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 关于JQueryMobile中Slider的一点研究

    滑动条 Slider                 给input的设置一个新的HTML5属性为type="range",可以给页面添加滑动条组件,可以指定它的value值(当前值 ...