problem:

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.

click to show more practice.

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.

Hide Tags

Divide and Conquer Array Dynamic
Programming

题意:找出一个序列的最大和连续子序列

thinking:

(1)这道题解法特别多:

方法1:将每个数和后一个数字相加,得到一个正负分布的序列,正数项对最大和子序列实用,作比較就可以

方法2:暴力匹配,两层循环,调用max()函数。时间复杂度O(n*n),也能够算出结果,可是提交超时

方法3:採用DP。时间复杂度O(n)

方法4:分治法。时间复杂度为nlog(n)

(2)本人实现了方法3 和方法4

code:

DP 法:

class Solution {
public:
int maxSubArray(int A[], int n) {
int sum=A[0];
int maxsum=A[0];
for(int i=1;i<n;i++)
{
if(sum<0) //DP核心
sum=0;
sum+=A[i];
maxsum=max(sum,maxsum);
}
return maxsum;
} };

分治法:

class Solution {
public:
int maxSubArray(int A[], int n) {
int ret=maxsub(A,0,n-1);
return ret; }
protected:
int maxsub(int A[], int start, int end)
{
int max_left=INT_MIN,max_mid=INT_MIN,max_right=INT_MIN;
if(start==end)
return A[start];
if(start+1==end)
{
int a=max(A[start],A[end]);
return a>(A[start]+A[end])?a:(A[start]+A[end]);
}
int mid=(start+end)/2;
int tmp_sum=A[mid];
max_mid=tmp_sum; int i=mid-1;
int j=mid+1;
while(i>=start) //难点在于当连续最大和子序列分布在mid的一側或两側时,怎么处理
{ tmp_sum+=A[i];
i--;
max_mid=max(max_mid,tmp_sum); }
if(max_mid>A[mid]) //推断是处于两側,还是处于一側
tmp_sum=max_mid;
else
tmp_sum=A[mid];
while(j<=end)
{ tmp_sum+=A[j];
j++;
max_mid=max(max_mid,tmp_sum); } max_left=max(max_left,maxsub(A,start,mid-1));//二分轮廓
max_right=max(max_right,maxsub(A,mid+1,end));
int tmp_max = max(max_left,max_right);
return max_mid>tmp_max?max_mid:tmp_max;
} };

leetcode || 53、Maximum Subarray的更多相关文章

  1. (LeetCode 53)Maximum Subarray

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

  2. 【LeetCode】053. Maximum Subarray

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

  3. [LeetCode]题53:Maximum Subarray

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

  4. LeetCode(53) Maximum Subarray

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

  5. 【LeetCode算法-53】Maximum Subarray

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

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

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

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

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

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

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

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

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

随机推荐

  1. IE8按F12不显示开发人员工具窗口

    转:http://www.cnblogs.com/micromouse/archive/2010/07/11/1775174.html 网上搜来的,记录一下,免得以后忘了 F12将开发人员工具启动后, ...

  2. 转载:Hadoop权威指南学习笔记

    转自:http://pieux.github.io/blog/2013-05-08-learn-hadoop-the-definitive-guide.html 1 前言 Hadoop的内部工作机制: ...

  3. android 布局居中

    android:layout_alignParentLeft="true" 位于父容器左上角 android:layout_alignParentBottom, android:l ...

  4. Apache benchmark对网站进行压力测试

    Apache Benchmark下载:http://down.tech.sina.com.cn/page/3132.html ab 的全称是 ApacheBench , 是 Apache 附带的一个小 ...

  5. 【C++对象模型】构造函数语意学之二 拷贝构造函数

    关于默认拷贝构造函数,有一点和默认构造函数类似,就是编译器只有在[需要的时候]才去合成默认的拷贝构造函数. 在什么时候才是[需要的时候]呢? 也就是类不展现[bitwise copy semantic ...

  6. LeetCode题解——4Sum

    题目: 给定一个数组,找出其中和为0的所有4个数组合,每个组合内的4个数非递降. 解法: ①先排序,然后利用4个指针,前两个遍历,后两个在第二个指针之后的部分里夹逼,时间O(N3). ②或者利用一个哈 ...

  7. public private protected和默认的区别(转自百度)

    public private protected和默认的区别 Java中对类以及类中的成员变量和成员方法通过访问控制符(access specifier)进行区分控制.刚学Java语言的同学可能对pu ...

  8. RabbitMQ三种Exchange模式(fanout,direct,topic)的特性 -摘自网络

    RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...

  9. Glibc辅助运行库 (C RunTime Library): crt0.o,crt1.o,crti.o crtn.o,crtbegin.o crtend.o

    crt1.o, crti.o, crtbegin.o, crtend.o, crtn.o 等目标文件和daemon.o(由我们自己的C程序文件产生)链接成一个执行文件.前面这5个目标文件的作用分别是启 ...

  10. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...