Maximum Subarray 连续子数组最大和

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.

此题为经典的求连续子数组最大和问题,除了暴力的解法,想办法用O(n)时间来做。

直接贴代码:

 1 class Solution {
 2 public:
 3     int maxSubArray(int A[], int n) {
 4         if(A == NULL || n < 1)
 5             return -1;
 6
 7         int max = A[0];
 8         int temp = 0;
 9
10         for(int i = 0 ; i < n ; i++){
11             if(temp < 0)
12                 temp = A[i];
13             else
14                 temp += A[i];
15
16             if(temp > max)
17                 max = temp;
18         }
19
20         return max;
21
22     }
23 };
时间: 2024-08-03 17:28:12

Maximum Subarray 连续子数组最大和的相关文章

连续子数组最大和(转)

求一个数组的相加和最大的连续子数组 思路: 一直累加,只要大于0,就说明当前的“和”可以继续增大, 如果小于0了,说明“之前的最大和”已经不可能继续增大了,就从新开始, result=max{result+arr[i],arr[i]};显然,若result>0,则可以继续相加,否则,就重新开始. #include<stdio.h> #define INF 65535 int Maxsum(int * arr, int size) { int maxSum = -INF; int sum

经典算法——连续子数组最大和问题

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. 一.问题描述:输入一个整数数组,求数组中连续的子数组使其和最大

连续子数组最大和

题目来源:<剑指offer>面试题31.<编程之美>2.14 题目:输入一个整形数组,数组里有正数也有负数.数组中一个或连续多个整数组成一个子数组.求所有子数组的和的最大值 解法一:假设id代表自序列的一个起点,j代表终点.如果a[i]是负的,那么它不可能代表最优子序列的起点,因为任何包含a[i]的作为起点的子序列都可以通过a[i+1]作起点得到改进.类似地,任何负的子序列不可能是最优子序列的前缀.比如有数组{1,-2,3,10,-4,7,2,-5}.我们试着从头道尾逐个累加数组中

一维数组连续子数组最大和

题目描述 输入一个整型数组,数组里有正数也有负数.数组中一个或连续的多个整数组成一个子数组.求所有子数组的和的最大值. 输入描述 输入有多组数据,每组测试数据包括两行.第一行为一个整数n(0<=n<=100000),当n=0时,输入结束. 接下去的一行包含n个整数(我们保证所有整数属于[-1000,1000]). 输出描述 对应每个测试案例,需要输出一个整数单独一行,表示连续子向量的最大和. 输入样例 3 -1 -3 -2 输出样例 -1 测试代码 1 #include <stdio.h

动态规划求解连续子数组最大和问题(应该是新的描述方法?)

刚才看了下网上搜索到的TOP5使用动态规划解决此问题的代码,感觉没有突出动态规划的特点.所以自己思考了一番,提出如下解决方案: 首先再重复下动态规划的定义:将待求解的问题分解为若干个子问题(阶段),按顺序求解子阶段,前一子问题的解,为后一子问题的求解提供了有用的信息. 思考状态转移方程:设d[i]表示i个数形成的数组中最大的连续子数列和,数组arr[i] ,i=0...n-1; 那么d[i] 和 d[i-1]应该具备什么样的关系?显然,有两种情况, 一是arr[i]>0得到d[i] = max(

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

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. 典型的DP问题,递推条件还是想了有点长时间,代码如下所示: 1

剑指offer第31题 连续子数组最大和

#include<iostream> using namespace std; int maxsum(int *list,int i) { if(list==NULL||i<=0) return -1; int cursum=0; int maxsum=0;//假设最小可以是0个数字 for(int j=0;j<i;j++) { if(cursum<=0) cursum=list[j]; else cursum=cursum+list[j]; if(cursum>max

【剑指offer】连续子数组最大和

思路dp很清楚,就是要注意细节. int FindGreatestSumOfSubArray(vector<int> array) { if(array.empty()) return 0; int sum = array[0], tempsum = array[0]; //注意初始值 不能设为0 防止只有负数 for(int i = 1; i < array.size(); i++) //从1开始 因为0的情况在初始化时完成了 { tempsum = (tempsum < 0)

剑指Offer29 连续子数组最大和

1 /************************************************************************* 2 > File Name: 29_GreatestSumOfSubArray.c 3 > Author: Juntaran 4 > Mail: [email protected] 5 > Created Time: 2016年09月01日 星期四 20时37分22秒 6 *****************************