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. Icon cache rebuilding with Delphi(Delphi 清除Windows 图标缓存源代码)

    清除Windows图标缓存的代码: procedure RebuildIconCache; .... const sr_WindowMetrics='Control Panel\Desktop\Win ...

  2. Android按键添加和处理的方案

    Android按键添加和处理的方案  版本号 说明 作者 日期  1.0  Android按键添加和处理的方案 Sky Wang  2013/06/18        需求:Android机器上有个W ...

  3. UINavigationController 、UINavigationBar 、UINavigationItem 超清晰直观详解

    UINavigationController 部分 1. UINavigationController 是一个容器类.里面盛放的是UIViewController. 容器的意思是,如果你不放入UIVi ...

  4. jquery.jCal.js显示日历插件

    描述:日历插件jCal用于需要输入日期的表单文本框. 兼容浏览器:IE浏览器/Firefox/Google Chrome 官方链接: http://www.overset.com/2008/05/1 ...

  5. js alert换行

    <script type="text/javascript"> alert("hello \n world!"); </script>

  6. 新闻编辑室第三季/全集The Newsroom迅雷下载

    第三季 The Newsroom Season 3 (2014)看点:今日他们终于公布了续订第三季的消息,但同时也宣称第三季将会是<新闻编辑室>的最终季,对剧迷们来说可谓苦乐参半.讲述了一 ...

  7. Lua date转秒数

    之前写过一篇关于把秒转换成指定的日期格式 Lua date format   接到一个需求,需要从配置文件中读取活动显示时间段:startDate ~ endDate(格式为:yyyy-mm-dd H ...

  8. Material Designer的低版本兼容实现(四)—— ToolBar

       Toolbar其实是一个ActionBar的变体,大大扩展了Actionbar.我们可以像对待一个独立控件一样去使用ToolBar,可以将它放到屏幕的任何位置,不必拘泥于顶部,还可以将它改变高度 ...

  9. vue elementui switch开关控件的使用

    <el-switch @change="test" on-value="1" off-value="0" v-model=" ...

  10. 隐马尔可夫(HMM)、前/后向算法、Viterbi算法

    HMM的模型  图1 如上图所示,白色那一行描述由一个隐藏的马尔科夫链生成不可观测的状态随机序列,蓝紫色那一行是各个状态生成可观测的随机序列 话说,上面也是个贝叶斯网络,而贝叶斯网络中有这么一种,如下 ...