【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)
描述:
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(贪婪&分治)的更多相关文章
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式
原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least ...
- LeetCode之“动态规划”:Maximum Subarray
题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...
- [LeetCode&Python] Problem 53. Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode]题53:Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode(53) Maximum Subarray
题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...
随机推荐
- IOS设计模式浅析之适配器模式(Adapter)
引言 在项目开发中,有时候会遇到这样的一种情景:需要使用以前开发的“一些现存的对象”,但是新环境中要求的接口是这些现存对象所不满足的.怎样应对这种迁移的需求?使得可以复用这些对象,以满足新的应用环境, ...
- SVN如何新建用户并分配权限
打开SVN服务端,找到特定的项目目录,单击右键,然后点击属性: 在弹出的页面中,点击增加: 在增加的页面中,你可以选择之前已经创建的用户,也可以重新创建用户名和密码: 如果是选择已经有的用 ...
- OpenCV中Kinect的使用(1)
图像处理中一般为了更好的获取外部信息都会使用到Kinect,其优势在于除了传统的RGB摄像头之外,还拥有一个获取深度信息的3D深度感应器,因此可以获得外界物体的3维信息实现物体的跟踪.手势识别等各项功 ...
- Eclipse 创建 Java 类
打开新建 Java 类向导 你可以使用新建 Java 类向导来创建 Java 类,可以通过以下途径打开 Java 类向导: 点击 "File" 菜单并选择 New > Cla ...
- 最新win7系统安全稳定版
最新win7系统32位安全稳定版 V2016年2月,具有更安全.更稳定.更人性化等特点.集成最常用的装机软件,集成最全面的硬件驱动,精心挑选的系统维护工具,加上萝卜独有人性化的设计.是电脑城.个人.公 ...
- python3----字符串中的字符倒转
方法一,使用[::-1]: s = 'python' print(s[::-1]) 方法二,使用reverse()方法: n = list(s) n.reverse() print(''.join(n ...
- python3 - property的使用
传统的绑定属性值,会把属性暴露出去,而且无法检查参数是否合法,如下: class Test(object): def __int__(self,age): self.age = age 为了检查参数 ...
- webService通过response和request对象传输文件
<code class=" hljs java">package gacl.response.study; 2 3 import java.io.IOException ...
- C++ 基础知识回顾(string基础、智能指针、迭代器、容器类)
[1] string基础 [1.1] string 的构造 #include <iostream> #include <string> int main() { using n ...
- @tap的传参和对全局变量的修改 onTap方法的k-v参数同时传入;
小程序wepy <view class="weui-panel__bd" @tap="onTap(e)" data-tapkey="itemNa ...