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. 类命名空间与对象、实例的命名空间 and 面向对象的组合用法

    类命名空间与对象.实例的命名空间 创建一个类就会创建一个类的名称空间,用来存储类中定义的所有名字,这些名字称为类的属性 而类有两种属性:静态属性和动态属性 静态属性就是直接在类中定义的变量 动态属性就 ...

  2. CF617/E XOR and Favorite Number

    题目链接:http://codeforces.com/contest/617/problem/E 题意:给出一个长度为n(1e5)的序列,有m(1e5)次操作,每次操作选择一个L-R区间,然后输出符合 ...

  3. HihoCoder - 1756 打怪

    题面在这里! 拆成两个部分分别算显然比较简单. 前面一个部分排个序枚举最大值算就好啦. 后面的就相当于把每一种数值的贡献加起来,也可以在排完序之后的a[]上面直接算出来. #include<bi ...

  4. Java虚拟机类加载机制--概述

    一.虚拟机类概加载概述 虚拟机将描述类的Class文件加载到内存,并对数据进行校验,转换解析和初始化,最终形成可以直接被虚拟机使用的Java类型 Java语言支持动态加载和动态连接. 二.虚拟机加载类 ...

  5. 80.Vigenère密码(模拟)

    Vigenère密码(文件名vigenere.cpp   vigenere.in    vigenere.out) 题目描述 Description 16 世纪法国外交家Blaise de Vigen ...

  6. HAproxy + keepalived 实现双机热备

    一.HAProxy简介: HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点, ...

  7. ios学习笔记图片+图片解释(c语言 oc语言 ios控件 ios小项目 ios小功能 swift都有而且笔记完整喔)

    下面是目录其中ios文件夹包括了大部分ios控件的介绍和演示,swift的时完整版,可以学习完swift(这个看的是swift刚出来一周的视频截图,可能有点赶,但是完整),c语言和oc语言的也可以完整 ...

  8. Spartan-6 FPGA Configuration

    These configuration pins serve as the interface for a number of different configuration modes: • JTA ...

  9. springmvc 配置和spring配置?

    最近在接触mybatis,之间使用springmvc时,配置文件一直是,web.xml+XX-servlet.xml 的配置(xx为web.xml中servlet name名称).为了整合mybati ...

  10. andriod 下一个页面

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s ...