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. 没有msdtc服务的解决方法(sql server分布式事务挂掉的解决方法)

    没有msdtc服务的解决方法如下:1.删除注册表中的键:  开始 运行 regedit  打开注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic ...

  2. ecshop的数据库getRow、getAll、getOne区别

    ECShop没有使用一些开源的数据库操作类,比如adodb或者PEAR,而是封装了自己的实现.这样做的好处是实现非常轻量,大大减小了分发包的文件大小.另外,当网站需要做memcached缓存时,也可以 ...

  3. Redis(十八):Redis和队列

    概要 Redis不仅可作为缓存服务器,还可用作消息队列.它的列表类型天生支持用作消息队列.如下图所示: 由于Redis的列表是使用双向链表实现的,保存了头尾节点,所以在列表头尾两边插取元素都是非常快的 ...

  4. Qt 检验器 三种典型类的用法

    Qt提供了三个内置验证器类: QDoubleValidator, QIntValidator, QRegExpValidator. QDoubleValidator类: 提供了对浮点数的范围检查. Q ...

  5. ubuntu apt-get方式安装与卸载

    在ubuntu终端中安装软件: 安装软件 apt-get install softname1 softname2 softname3……卸载软件 apt-get remove softname1 so ...

  6. mplayer 全屏问题

    [root@ok home]# gedit ~/.mplayer/config # Write your default config options here! zoom=yes #加上这个参数!全 ...

  7. Memcached安装以及PHP的调用

    Memcached安装以及PHP的调用 [南京·10月17日]OSC源创会开始报名:Swift.大型移动项目构架分享 » 一:安装libevent 由于memcached安装时,需要使用libeven ...

  8. dubbo相关

    1 面试题:Dubbo中zookeeper做注册中心,如果注册中心集群都挂掉,发布者和订阅者之间还能通信么? 可以的,启动dubbo时,消费者会从zk拉取注册的生产者的地址接口等数据,缓存在本地.每次 ...

  9. 坑爹的A标签 href

    A标签 href在与click事件同时响应时,如果click事件有提交表单动作,href会阻拦表单提交,解决 1.去掉href 2.href="javascript:void();" ...

  10. php服务器环境变量

    可以把一些配置写到apache或nginx的配置里,然后在代码里判断环境变量来实现开发环境和线上环境的切换. 比如在本地可以 SetEnv APP_ENV local线上则 SetEnv APP_EN ...