Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.

More practice:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

法I:动态规划。max_local存储到i之前的sum,如果<0,表示之前的sum对之后只可能有负贡献,所以忽略,直接考虑nums[i]。max_global存储目前为止出现过的最大sum。

动态转移方程是:

局部最优:max_local= max(max_local+nums[i], nums[i])

全局最优:max_global= max(max_global, max_local)

class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max_local = nums[]; //maxSum may be negative, so can't simply write int curSum = 0
int max_global = nums[];
for(int i = ; i < nums.size(); i++){
max_local = max(max_local+nums[i],nums[i]);
max_global = max(max_global, max_local);
}
return max_global;
}
};

法II:分治法

将数组分为左右两段,分别找到最大子串和,然后还要从中间开始往两边扫描,求出最大和,比较三个和取最大值。

class Solution {
public:
int maxSubArray(vector<int>& nums) {
return binarySearch(nums,,nums.size()-);
} int binarySearch(vector<int>& nums, int left, int right){
//right=left<=1的两种情况都要讨论
if(left==right)
return nums[left];
if(right-left == ){
return max(max(nums[left],nums[right]),nums[left]+nums[right]);
} int mid = left +((right-left)>>);
int leftmax = binarySearch(nums, left, mid);
int rightmax = binarySearch(nums, mid+, right); int curLeft = nums[mid],curRight=nums[mid+], maxLeft=nums[mid], maxRight=nums[mid+];
for(int i = mid-; i>=left; i--){
curLeft+=nums[i];
maxLeft = max(curLeft,maxLeft);
}
for(int i = mid+; i <= right; i++){
curRight+=nums[i];
maxRight = max(curRight,maxRight);
} return max(max(leftmax,rightmax),maxLeft+maxRight);
}
};

53. Maximum Subarray (Array; DP)的更多相关文章

  1. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  2. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  3. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  4. 41. leetcode 53. Maximum Subarray

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

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  7. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

  8. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  9. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

随机推荐

  1. python 使用selenium和requests爬取页面数据

    目的:获取某网站某用户下市场大于1000秒的视频信息 1.本想通过接口获得结果,但是使用post发送信息到接口,提示服务端错误. 2.通过requests获取页面结果,使用html解析工具,发现麻烦而 ...

  2. 1006 Sign In and Sign Out (25 分)

    1006 Sign In and Sign Out (25 分) At the beginning of every day, the first person who signs in the co ...

  3. 牛逼的lsof命令!!!

    linux lsof命令详解 简介 lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访 ...

  4. JpGraph使用详解http://5ydycm.blog.51cto.com/115934/177498 http://www.cnblogs.com/txw1958/archive/2013/08/18/php-charts.html

    下载 在官方网站 http://www.aditus.nu/jpgraph/ 下载jpgraph,其中1.X系列是用于PHP4的,2.X系列是用于PHP5的. 安装 将下载的得到的jpgraph压缩文 ...

  5. 存储设备的DDP功能详解

    http://blog.csdn.net/u013394982/article/details/18259015 DDP功能,即Dynamic Disk Pool,它是除了现有的RAID0,1,10, ...

  6. 暴搜 - Codeforces Round #327 (Div. 2) E. Three States

    E. Three States Problem's Link Mean: 在一个N*M的方格内,有五种字符:'1','2','3','.','#'. 现在要你在'.'的地方修路,使得至少存在一个块'1 ...

  7. uva-10474-枚举-水题

    题意:看代码 #include<stdio.h> #include<iostream> #include<sstream> #include<queue> ...

  8. nginx、TP框架实现兼容pathinfo和rewrite两种url访问方式

    环境:centos7,yum安装的nginx1.10.php-fpm,tp3.2 本方法只需要配置nginx.conf的一个文件就可以支持pathinfo和rewrite两种url访问方式 vim / ...

  9. Latex 箭头、下标、符号上下写文字、正方形和三角形

    0. hat   $abc\hat def$  $ab\widehat{cde}f$ 1. 箭头上的文字    $\underrightarrow{\text{你的文字}}$  ($A \xlefta ...

  10. Security4.1.3实现根据请求跳转不同登录页以及登录后根据权限跳转到不同页配置

    参考博客:https://blog.csdn.net/honghailiang888/article/details/53765508