【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 ...
随机推荐
- 华为终端-新浪微博联合创新,3D建模+AR 成就全新社交体验
近日,全球首款搭载3D感知摄像头的手机华为Mate 20发布. 通过Mate 20自带的景深摄像头及麒麟980的NPU加速能力,手机能够在获取物体表面信息后,完成高速的精细化3D建模. 那么,如何让3 ...
- hdu2068 RPG的错排 错排+组合
RPG的错排 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 日志系统之扩展Flume-LineDeserializer
本人博客文章如未特别注明皆为原创.如有转载请注明出处:http://blog.csdn.net/yanghua_kobe/article/details/46595401 继续闲聊日志系统,在之前的博 ...
- 前端webview开发中遇到的一些问题及其解决方法
最近做了一个webview中的兑换页面,本来以为很简单,想不到遇到了远远超出预期数量的BUG,记下来,以备后患. 1 inline-block元素折行 BUG描述:现在我有三个DIV,要在一列等宽排列 ...
- Linux Linux常用命令二
whoami 我是谁命令 --该命令用户查看当前系统当前账号的用户名 --由于系统管理员通常需要使用多种身份登录系统,李儒通常使用普通用户登录系统,然后再以su命令切换到root身份对系统进行灌篮.这 ...
- python 爬虫4 cookies
Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密) 比如说有些网站需要登录后才能访问某个页面,在登录之前,你想抓取某个页面内容是不允许的.那么 ...
- What is special about /dev/tty?
ls -la /dev/tty shows the output: crw-rw-rw- 1 root tty 5, 0 Dec 14 22:21 /dev/tty The 'c' means it' ...
- hadoop2.4.1伪分布模式部署
hadoop2.4.1伪分布模式部署 (承接上一篇hadoop2.4.1-src的编译安装继续配置:http://www.cnblogs.com/wrencai/p/3897438.html) 感谢: ...
- AsyncTask机制学习
其内容可以参考http://blog.csdn.net/webgeek/article/details/17298237 ,首先创建一个AsyncTask类 class GetFaceDetectTa ...
- SQL.Cookbook 读书笔记5 元数据查询
第五章 元数据查询 查询数据库本身信息 表结构 索引等 5.1 查询test库下的所有表信息 MYSQL SELECT * from information_schema.`TABLES` WHERE ...