【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 ...
随机推荐
- YII用户注冊和用户登录(二)之登录和注冊在视图通过表单使用YII小物件并分析
2 登录和注冊在视图通过表单使用YII小物件并分析 <?php $form = $this -> beginWidget('CActiveForm', array( 'enableClie ...
- 解决easyui tabs中href无法跨域跳转
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content ...
- zmq重点
The zmq_msg_send(3) method does not actually send the message to the socket connection(s). It queues ...
- python3----字符串格式化(format)
用法: 它通过{}和:来代替传统%方式 1.使用位置参数 要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 ...
- 1、aritcMS-环境搭建-设置布局-布局测试
在开始项目之前,准备开发工具等请参考easyUI章节. 首先.在获取broPHP框架的时候(可以在网上下载).在brophp文件夹同级目录下新建admin.php,以及index.php admin. ...
- linux终端常用命令
常用的信息显示命令 命令#pwd 用于在屏幕上输出当前的工作目录. 命令#stat 用于显示指定文件的相关信息. 命令#uname -a 用于显示操作系统信息. 命令#hostname 用于显示当前本 ...
- 第二篇:尽可能使用 const
前言 const 关键字是常量修辞符,如果要告知编译器某个变量在程序中不会发生改变,则可将其声明为 const. 但,对 const 关键字的认识不能仅仅停留在这一层 - 它提供了很多更强大的功能. ...
- (分享)Linux服务器如何防止中木马
大家的windows机器可能经常装一些杀毒软件或者什么的来防止中毒,然而在Linux上我们应该怎么防止这些呢? 在面试过程中我们也经常遇到该类问题,那么我们应该怎么回答才显得既有逻辑又有深度呢? 首先 ...
- 调用第三方物流公司API即时查询物流信息
主要是利用快递鸟提供的物流服务,通过对接快递鸟的API,调用即时查询接口,获取物流信息. 这里采用java语言,调用快递鸟的接口为例.步骤如下: 1.首先,得去快递鸟的官方网站注册一个账号并进行实名认 ...
- 巨蟒python全栈开发linux之centos1
1.linux服务器介绍 2.linux介绍 3.linux命令学习 linux默认有一个超级用户root,就是linux的皇帝 注意:我的用户名是s18,密码是centos 我们输入密码,点击解锁( ...