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.

题目:

最大子数组和

思路:

1、暴力枚举

起点:i=0,...,n-1;终点:j=i,....,n-1;依次求[i,j]区间的和,时间复杂度O(n^3)

2、优化枚举

起点:i=0,...,n-1;终点:j=i,....,n-1;累计求[i,j]区间的和,时间复杂度O(n^2)

3、分治算法

分:两个等长的子数组,分别求解,复杂度O(nlogn)

合:求包含中间点的最大子数组之和,复杂度O(n)

时间复杂度:O(nlogn)

4、动态规划

假设dp[i]表示以a[i]结尾的最大子数组和,那么

状态转移方程:

dp[i]=max(dp[i-1]+a[i],a[i])

  • 包含a[0,i-1]:dp[i-1]+a[i]
  • 不包含a[0,i-1]:a[i]

初始值:

dp[0]=a[0]

复杂度:

时间复杂度:O(n),空间复杂度:O(n)

空间优化:

dp[i]只与dp[i-1]有关,因此状态转移方程优化为:

best=max(best+a[i],a[i])

其实这里的动态规划实现的是一种简单的逻辑,即前面的数组和大于0,则加上,小于或等于0,则放弃。

if(cur>0)
  cur+=A[i];
else
  cur=A[i];

5、前缀数组和

定义:sum[i]=a[0]+a[1]+...+a[i]

sum(A[i....j])=sum[j]-sum[i-1]

对于数组A,以A[i]结尾的最大子数组和为sum[i]-min(sum(k)),k=0...i-1,因此需保存每一步计算中的最小sum值。

依次计算以A[i]结尾的最大子数组和,然后保留其最大值即可,详见代码。

代码:

只实现分治、动态规划以及前缀和三种思路

1、分治

class Solution {
public:
int maxSubArray(int A[], int n) {
if(n==1)
return A[0]; int mid=n/2;
int left=maxSubArray(A,mid);
int right=maxSubArray(A+mid,n-mid);
int ans=max(left,right); int cur=A[mid-1];
int tmp=cur;
for(int i=mid-2;i>=0;i--){
cur+=A[i];
if(cur>tmp)
tmp=cur;
}
cur=tmp;
for(int i=mid;i<n;i++){
cur+=A[i];
if(cur>tmp)
tmp=cur;
} return max(ans,tmp); }
};

2、动态规划

class Solution {
public:
int maxSubArray(int A[], int n) {
int cur=A[0];
int max=A[0];
for(int i=1;i<n;i++){
if(cur>0)
cur+=A[i];
else
cur=A[i];
if(cur>max)
max=cur;
}
return max;
}
};
class Solution {
public:
int maxSubArray(int A[], int n) {
int endhere=A[0];
int ans=A[0]; for(int i=1;i<n;i++){
endhere=max(endhere+A[i],A[i]);
ans=max(ans,endhere);
} return ans;
}
};

3、前缀数组和

class Solution {
public:
int maxSubArray(int A[], int n) {
int sum=A[0];
int minSum=min(0,sum);
int ans=A[0]; for(int i=1;i<n;i++){
sum+=A[i];
ans=max(ans,sum-minSum);
minSum=min(minSum,sum);
} return ans;
}
};

  

(LeetCode 53)Maximum Subarray的更多相关文章

  1. leetcode || 53、Maximum Subarray

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

  2. LeetCode(53) Maximum Subarray

    题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...

  3. (Problem 53)Combinatoric selections

    There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...

  4. 【算法】LeetCode算法题-Maximum Subarray

    这是悦乐书的第154次更新,第156篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第13题(顺位题号是53).给定一个整数数组nums,找出一个最大和,此和是由数组中索引 ...

  5. (LeetCode 78)SubSets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  6. (LeetCode 72)Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  7. 《从零开始学Swift》学习笔记(Day 53)——do-try-catch错误处理模式

    原创文章,欢迎转载.转载请注明:关东升的博客 Swift 1.x的错误处理模式存在很多弊端,例如:为了在编程时候省事,给error参数传递一个nil,或者方法调用完成后不去判断error是否为nil, ...

  8. 【LeetCode】053. Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  9. LeetCode OJ:Maximum Subarray(子数组最大值)

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

随机推荐

  1. 解释Crypto模块怎么就这么"皮"?No module named "Crypto"

    https://www.cnblogs.com/fawaikuangtu123/p/9761943.html python版本:python3.6,系统:win7 1.pip install cryp ...

  2. Vue 2.0学习(六)内置指令

    基本指令 1.v-cloak v-cloak不需要表达式,它会在Vue实例结束编译时从绑定的HTML元素上移除,经常和CSS的display:none配合使用. <div id="ap ...

  3. Hibernate 基于外键的单项一对一关联映射

    在开发过程中很多时候会用到表与表之间一对一的关联关系,本文简单介绍在Hibernate4中单项一对一的关联映射. 1.设计表结构 2.创建Person对象 3.创建IdCard对象 4.写hbm.xm ...

  4. 【BZOJ 2119】 2119: 股市的预测 (后缀数组+分块+RMQ)

    2119: 股市的预测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 404  Solved: 188 Description 墨墨的妈妈热爱炒股,她 ...

  5. logN判点是否在凸多边形内 HRBUSTOJ1429

    就是利用叉积的性质,如果向量A1到向量A2是顺时针则叉积为负反之为正. 然后我们可以二分的判断找到一个点恰被两条射线夹在一起. 然后我们再判断是否l,r这两个点所连直线与点的关系. 具体资料可以参照这 ...

  6. 51nod1624 取余最长路 前缀和 + set

    由于只有3行,因此只会会换行2次,假设$x, y$分别为这两次的换行点 那么答案为$S[1][x] +S[2][y] - S[2][x - 1] + S[3][n] - S[3][y - 1]$ 其中 ...

  7. 【最小表示法】BZOJ2176-Strange string(unsigned char!!!)

    [题目大意] 给定一个字符串S = {S1, S2, S3 … Sn}, 如果在串SS中, 子串T(|T| = n)为所有长度为n的SS的字串中最小的(字符串的比较), 则称T为”奇怪的字串”. 你的 ...

  8. java23种设计模式之一: 代理模式(动态代理)

    在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...

  9. [转]Intel haxm安装失败问题解决

    在安装Intel haxm为安卓模拟器加速时,会遇到提示VT-X未开启问题,问题提示如下图 工具/原料 Intel haxm 安卓模拟器 方法/步骤 1 确认你的处理器是否是Intel的,如果是AMD ...

  10. 如何解决…has been modified since the precompiled header… was built的问题

    如何解决…has been modified since the precompiled header… was built 的问题 xcode5.1在程序中报错: File '/Applicatio ...