1. 暴力枚举

2. “聪明”枚举

3. 分治法

分:两个基本等长的子数组,分别求解T(n/2)

合:跨中心点的最大子数组合(枚举)O(n)

时间复杂度:O(n*logn)

 class Solution {
public:
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
int maxSubArray(vector<int> nums) {
// write your code here
int size = nums.size();
if (size == ) {
return nums[];
}
int *data = nums.data();
return helper(data, size);
}
int helper(int *data, int n) {
if ( n == ) {
return data[];
}
int mid = n >> ;
int ans = max(helper(data, mid), helper(data + mid, n - mid));
int now = data[mid - ], may = now;
for (int i = mid - ; i >= ; i--) {
may = max(may, now += data[i]);
}
now = may;
for (int i = mid; i < n; i++) {
may = max(may, now += data[i]);
}
return max(ans, may);
}
};

4. dp(不枚举子数组,枚举方案)

dp[i]表示以a[i]结尾的最大子数组的和

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

  包含a[i-1]:dp[i-1]+a[i]

  不包含a[i-1]:a[i]

初值:dp[0] = a[0]

答案:最大的dp[0...n-1]

时间:O(n)

空间:O(n)

空间优化:dp[i]要存吗?

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

  answer = max(endHere, answer)

优化后的空间:O(1)

 class Solution {
public:
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
int maxSubArray(vector<int> nums) {
// write your code here
int size = nums.size();
if (size == ) {
return nums[];
}
vector<int> dp(size);
dp[] = nums[];
int ans = dp[];
for (int i=; i<size; i++) {
dp[i] = max(dp[i - ] + nums[i], nums[i]);
ans = max(ans, dp[i]);
}
return ans;
}
};

空间优化

 class Solution {
public:
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
int maxSubArray(vector<int> nums) {
// write your code here
int size = nums.size();
if (size == ) {
return nums[];
}
int endHere = nums[];
int ans = nums[];
for (int i=; i<size; i++) {
endHere = max(endHere + nums[i], nums[i]);
ans = max(ans, endHere);
}
return ans;
}
};

5. 另外一种线性枚举

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

     sum[-1] = 0

则对0<=i<=j:

  a[i] + a[i+1] + ... + a[j] = sum[j] - sum[i-1]

我们就是要求这样一个最大值:

  对j我们可以求得当前的sum[j],取的i-1一定是之前最小的sum值,用一个变量记录sum的最小值

  时间:O(n)

  空间:O(1)

 class Solution {
public:
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
int maxSubArray(vector<int> nums) {
// write your code here
int size = nums.size();
if (size == ) {
return nums[];
}
int sum = nums[];
int minSum = min(, sum);
int ans = nums[];
for (int i = ; i < size; ++i) {
sum += nums[i];
ans = max(ans, sum - minSum);
minSum = min(minSum, sum);
}
return ans;
}
};

LintCode: Maximum Subarray的更多相关文章

  1. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  2. Lintcode: Maximum Subarray III

    Given an array of integers and a number k, find k non-overlapping subarrays which have the largest s ...

  3. Lintcode: Maximum Subarray Difference

    Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is ...

  4. Lintcode: Maximum Subarray II

    Given an array of integers, find two non-overlapping subarrays which have the largest sum. The numbe ...

  5. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  6. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  7. LEETCODE —— Maximum Subarray [一维DP]

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

  8. 【leetcode】Maximum Subarray

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

  9. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

随机推荐

  1. spring-boot项目在eclipse中指定配置文件启动

    原文:https://blog.csdn.net/ztx114/article/details/80076339 如下图我的项目有三个配置文件,假如我向指定用application-test.yml启 ...

  2. IOS开发之——objective-c与javascript交互

    原文:http://blog.csdn.net/pjk1129/article/details/6936545 在写 JavaScript 的时候,可以使用一个叫做 window 的对象,像是我们想要 ...

  3. UICollectionView 相关

    当数据不多,collectionView.contentSize小于collectionView.frame.size的时候,UICollectionView是不会滚动的 self.Cov.alway ...

  4. uitextfield 设置为密码框显示

    uitextfield 设置为密码框显示: 在xib中,将文本secure的复选框选中即可.

  5. C#编程(十九)----------部分类

    部分类 C#中使用关键字partial把类,结构或结构放在多个文件中.一般情况下,一个类全部驻留在单个文件中.但有时候,多个开发人员需要访问同一个类,或者某种类型的代码生成器生成了一个类的某部分,所以 ...

  6. Scala从零開始:使用Intellij IDEA写hello world

    引言 在之前的文章中,我们介绍了怎样使用Scala IDE也就是eclipse中集成的Scala开发插件来进行Scala语言程序的开发,在使用了一段时间之后,发现eclipse对Scala的支持并非非 ...

  7. 猫都能学会的Unity3D Shader入门指南(二)

    关于本系列 这是Unity3D Shader入门指南系列的第二篇,本系列面向的对象是新接触Shader开发的Unity3D使用者,因为我本身自己也是Shader初学者,因此可能会存在错误或者疏漏,如果 ...

  8. java.util.Vector排序

    Vector的排序: import java.util.*; class MyCompare implements Comparator //实现Comparator,定义自己的比较方法{public ...

  9. maven的groupid和artifactId

    groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找. groupId一般分为多个段 ...

  10. yolo源码解析(三)

    七 测试网络 模型测试包含于test.py文件,Detector类的image_detector()函数用于检测目标. import os import cv2 import argparse imp ...