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: ...
随机推荐
- HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot-报错解决方法
操作:jsp文件里面添加<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" % ...
- Logstash之二:原理
一.Logstash 介绍 Logstash 是一款强大的数据处理工具,它可以实现数据传输,格式处理,格式化输出,还有强大的插件功能,常用于日志处理. 二.工作流程 Logstash 工作的三个阶段: ...
- SonarQube
代码质量管理 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具.与持续集成工具(例如 Hudson/Jenkins 等)不 ...
- autocomplete 自动填充 combobox
目录(?)[-] autocomplete有两种 一种 是 jquery ui里的 autocomplete httpjqueryuicomautocomplete 另一种是 ASPNET AJAX ...
- 学习MongoDB 三: MongoDB无法启动的解决方法
一简介 我们之前介绍了MongoDB入门(安装与配置),我们今天在打开MongDB时,我们先运行cmd.exe进入dos命令界面,然后进入cd D:\mongodb\bin目录下,启动服务或者mon ...
- 显式锁(三)读写锁ReadWriteLock
前言: 上一篇文章,已经很详细地介绍了 显式锁Lock 以及 其常用的实现方式- - ReetrantLock(重入锁),本文将介绍另一种显式锁 - - 读写锁ReadWriteLock. ...
- css的样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [Illuminate\Database\QueryException] SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using pas sword: NO) (SQL: select * from information_schema.tables where table_schema = la
[Illuminate\Database\QueryException] SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost ...
- HTML5 Canvas ( 创建图形对象 ) createImageData
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Unable to open file '.RES'
Unable to open file '.RES' 另存工程,带来的隐患,工程图标也改不了. 搜索发现源码里某个man.cpp里带了prgram resource aaa.res,换成新工程文件名 ...