描述:

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

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

思路一:动态规划

因为这是个优化问题。并且问题可以分解为一个个子问题,所以利用DP来解决此问题是一种很好的解决方案:

dp[i-1]到dp[i]的转换分两种情况:

        1)dp[i-1] > 0: 当大于0时,dp[i] = dp[i-1] + num[i]

        2) dp[i-1] < 0:当大于0时,dp[i] = 0  (因为此时将剔除dp[i-1]的影响)

class Solution {
public:
int maxSubArray(vector<int>& nums) {
vector<int> dp(nums.size());
dp[] = nums[];
int max_ans = dp[];
for(int i = ;i<nums.size();++i){
dp[i] = nums[i] + (dp[i-] > ? dp[i-] : );
max_ans = max(max_ans, dp[i]);
}
return max_ans;
}
};

思路二:贪婪

从左到右汇总数组时找到总和最优解,和dp类似,但是我们这里不保存dp的状态,只记录临时sum和最大sum

class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sum = ;
int ans;
ans = nums[];
for(int i = ;i<nums.size();i++){
sum+=nums[i];
ans = max(ans,sum);
sum = max(sum,);
}
return ans;
}
};

思路三:分治法

分治法的思路是将问题不断二分,分到不能再分,然后再将计算完的数据整合归一,最后得出最优解,这里,如图所示,将数组不断二分,然后取出每一段的最大sum,然后传回总函数,然后输出最优解

class Solution {
public:
int maxSubArray(vector<int>& nums) {
if(nums.size()==) return ;
return maxSubArray(nums, , nums.size() - );
} // l代表数组左端low,h代表数组右端high,返回最大sum
int maxSubArray(vector<int>& arr, int l, int h)
{
// Base Case: Only one element
if (l == h)
return arr[l]; // Find middle point
int m = (l + h)/; /* Return maximum of following three possible cases
a) Maximum subarray sum in left half
b) Maximum subarray sum in right half
c) Maximum subarray sum such that the subarray crosses the midpoint */
return max(max(maxSubArray(arr, l, m), //最左侧数组求最大sum
maxSubArray(arr, m+, h)), //对右侧数组求最大sum ,之后求左右的最大值
maxCrossingSum(arr, l, m, h)); //对整个数组以mid为分界线求最大sum
} int maxCrossingSum(vector<int>& arr, int l, int m, int h)
{
// Include elements on left of mid.
int sum = ;
int left_sum = INT_MIN;
for (int i = m; i >= l; i--)
{
sum = sum + arr[i];
if (sum > left_sum)
left_sum = sum;
} // Include elements on right of mid
sum = ;
int right_sum = INT_MIN;
for (int i = m+; i <= h; i++)
{
sum = sum + arr[i];
if (sum > right_sum)
right_sum = sum;
} // Return sum of elements on left and right of mid
return left_sum + right_sum;
}
};

【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)的更多相关文章

  1. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

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

  2. LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

    Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...

  3. LeetCode练题——53. Maximum Subarray

    1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...

  4. LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式

    原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least ...

  5. LeetCode之“动态规划”:Maximum Subarray

    题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...

  6. [LeetCode&Python] Problem 53. Maximum Subarray

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

  7. [LeetCode]题53:Maximum Subarray

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

  8. [leetcode.com]算法题目 - Maximum Subarray

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

  9. LeetCode(53) Maximum Subarray

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

随机推荐

  1. linux用户密码生成

    linux账户保存在/etc/passwd,密码保存在/etc/shadow. 通过man 5 passwd,man 5 shadow可查看文件中各字段含义. encrypted password   ...

  2. PHP编译选项

    PHP安装 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/u ...

  3. Kafka具体解释五、Kafka Consumer的底层API- SimpleConsumer

    1.Kafka提供了两套API给Consumer The high-level Consumer API The SimpleConsumer API 第一种高度抽象的Consumer API,它使用 ...

  4. jvm(13)-线程安全与锁优化

    [0]README 0.1)本文部分文字转自“深入理解jvm”, 旨在学习 线程安全与锁优化 的基础知识: 0.2)本文知识对于理解 java并发编程非常有用,个人觉得,所以我总结的很详细: [1]概 ...

  5. KMP hihoCoder1015 KMP算法

    人太蠢,,看了一天的KMP.. 刚開始看训练指南的,,后来才惊奇的发现原来刘汝佳写的f数组并非Next数组! 总认为和之前看过的全然不一样.. . 后来又百度了一下KMP,研究了非常久,然后用自己的逻 ...

  6. JS异步笔记

    Promise 最早接触异步是在.net中,当时还是比较流行使用基于控件的BackgroundWorker,其自身通过子线程的方式来异步处理一些情况,并且封装了一些功能与主线程通信.后来,开始使用Th ...

  7. Webkit内核探究【2】——css简介

    注:[转载请注明文章来源.保持原样] 出处:http://www.cnblogs.com/jyli/archive/2010/01/31/1660364.html 作者:李嘉昱 CSS在Webkit中 ...

  8. week 5: ;Lasso regression & coordinate descent

    笔记. 岭回归, 计算回归系数时使( RSS(w)+λ||w||2) 最小 岭回归的结果会是所有的特征的weight都较小,但大多数又不完全为零. 而实际情况中,有的特征的确与输出值相关程度很高,we ...

  9. React ES5 (createClass) 和 ES6 (class)

    https://www.w3cplus.com/react/react-es5-createclass-vs-es6-classes.html http://blog.csdn.net/shaleil ...

  10. UVA 10319 - Manhattan(2-SET)

    UVA 10319 - Manhattan 题目链接 题意:一个城市,有南北和东西街道.每种街道都是单行道,如今给定几个起点和终点.要求起点和终点必须最多转一次弯能够到达,问能否够满足全部的起点终点 ...