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: ...
随机推荐
- java发送http请求,内容为xml格式&&传统URI类请求
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...
- Spring MVC 处理模型数据
SpringMVC 处理模型数据: 1 controller接收pojo: <form action="save" method="get"> &l ...
- unity3d中给GameObject绑定脚本的代码
一.获取GameObject 1.GameObject.Find() 通过场景里面的名子或者一个路径直接获取游戏对象. GameObject root = GameObject.Find(“Ga ...
- Jenkins Error cloning remote repo 'origin', slave node
使用jenkins pull git上的代码,在job中配置好源码管理后,构建时出现如题错误提示: 网上的资料几乎都是在说SSH的配置问题,因为博主项目建立在本地的git服务器上,所以在源码管理中选择 ...
- Jenkins 邮件发送设置(jenkins自带邮件设置)
首先进入系统设置,找到Jenkins Location部分 这里设置 系统管理员邮件地址,然后设置邮件通知部分,这里为了方便我使用了QQ邮箱(作为发送邮件地址) 这里的 用户名 必须与上面的 系统管理 ...
- selenium+python自动化87-Chrome浏览器静默模式启动(headless)
前言 selenium+phantomjs可以打开无界面的浏览器,实现静默模式启动浏览器完成自动化测试,这个模式是极好的,不需要占用电脑的屏幕. 但是呢,phantomjs这个坑还是比较多的,并且遇到 ...
- placeholder测试
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 2. 修改myeclipse默认用户和时间
方法一:将 @author 属性写死 . 通过菜单 Window->Preference 打开参数设置面板,然后选择: 1.Java -> Code Style -> Code Te ...
- 关于BeautifulSoup类中的tag对象的string和text属性
<dl> <dt> 今开 </dt><dd class="s-down">3.87</dd> </dl> & ...
- leetcode551
public class Solution { public bool CheckRecord(string s) { ; ; ; var preChar = '\0'; ; i < s.Len ...