53. Maximum Subarray (Array; DP)
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4]
,
the contiguous subarray [4,−1,2,1]
has the largest sum = 6
.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
法I:动态规划。max_local存储到i之前的sum,如果<0,表示之前的sum对之后只可能有负贡献,所以忽略,直接考虑nums[i]。max_global存储目前为止出现过的最大sum。
动态转移方程是:
局部最优:max_local= max(max_local+nums[i], nums[i])
全局最优:max_global= max(max_global, max_local)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max_local = nums[]; //maxSum may be negative, so can't simply write int curSum = 0
int max_global = nums[];
for(int i = ; i < nums.size(); i++){
max_local = max(max_local+nums[i],nums[i]);
max_global = max(max_global, max_local);
}
return max_global;
}
};
法II:分治法
将数组分为左右两段,分别找到最大子串和,然后还要从中间开始往两边扫描,求出最大和,比较三个和取最大值。
class Solution {
public:
int maxSubArray(vector<int>& nums) {
return binarySearch(nums,,nums.size()-);
} int binarySearch(vector<int>& nums, int left, int right){
//right=left<=1的两种情况都要讨论
if(left==right)
return nums[left];
if(right-left == ){
return max(max(nums[left],nums[right]),nums[left]+nums[right]);
} int mid = left +((right-left)>>);
int leftmax = binarySearch(nums, left, mid);
int rightmax = binarySearch(nums, mid+, right); int curLeft = nums[mid],curRight=nums[mid+], maxLeft=nums[mid], maxRight=nums[mid+];
for(int i = mid-; i>=left; i--){
curLeft+=nums[i];
maxLeft = max(curLeft,maxLeft);
}
for(int i = mid+; i <= right; i++){
curRight+=nums[i];
maxRight = max(curRight,maxRight);
} return max(max(leftmax,rightmax),maxLeft+maxRight);
}
};
53. Maximum Subarray (Array; DP)的更多相关文章
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- pychar入门参考教材
参考: http://blog.csdn.net/chenggong2dm/article/category/6137682 让不同py文件运行,直接在文件的标签处右键run即可
- 史上最详细 Python第三方库添加方法 and 错误解决方法
(1):如何添加python第三方库(方法一): File ->> Settings... ->> Project Interpreter (2):如何添加python第三方库 ...
- Splunk 交流
1. 初识splunk Splunk Enterprise Splunk Free Splunk Universal Forwarder,通用转发器
- 企业常用的RPC框架比较
RPC框架比较 语言 协议 服务治理 社区 机构 Hessian 多语言 hessian(二进制) – 不活跃 Caucho Thrift 多语言 thrift – 活跃 Apache Fin ...
- python的return self的用法
转载:https://blog.csdn.net/jclian91/article/details/81238782 class foo: def __init__(self): self.m = 0 ...
- Install hadoop on windows(non-virtual machine, such cygwin)
DownloadBefore starting make sure you have this two softwares Hadoop 2.7.1 Java – Jdk 1.7+ Extract d ...
- 用Dockerfile生成docker image
在docker的官方php镜像中,有独立的php和apache版本的,这里尝试用php-fpm7.2.1(alpine3.7)作为基础镜像,在把nginx1.13.8加进去. 第一步:拉取php镜像: ...
- 搭建Hive 2.1.1 基于Hadoop 2.6.1 和 Ubuntu 16.0.4 记录
Hadoop Hive Hbase 对应版本 Hive官网下载 我们以Hadoop版本作为参考适配Hive Hbase即可, Hadoop版本是2.6.1 所以可以选择Hive1.2.1以上版 ...
- jieba库及wordcloud库的使用
知识内容: 1.jieba库的使用 2.wordcloud库的使用 参考资料: https://github.com/fxsjy/jieba https://blog.csdn.net/fontthr ...
- 用dataset保存数据注意的问题
Private Function Save() As Boolean Try Dim dschgs As DataSet = ds.GetChanges(DataRowState.Added + Da ...