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: ...
随机推荐
- 学习笔记之Jira
Jira | Issue & Project Tracking Software | Atlassian https://www.atlassian.com/software/jira The ...
- [原]System.IO.Path.Combine 路径合并
使用 ILSpy 工具查看了 System.IO.Path 类中的 Combine 方法 对它的功能有点不放心,原方法实现如下: // System.IO.Path /// <summary&g ...
- tcp_tw_recycle和tcp_timestamps导致connect失败问题
把服务里面的net.ipv4.tcp_timestamps这个参数设置为0后已经可以正常telnet通了. 具体设置方法: 在/etc/sysctl.conf 里面加入 net.ipv4.tcp_t ...
- JSON Web Token的使用(转载)
定义 JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用JWT在用户和服务器之间传递安全可靠的信息. 适用场景 1.用于向Web应用传递一些非敏感信息.例如完成加好友.下 ...
- Ubuntu登录系统失败的解决方案
问题一: 只能用guest用户登录下,如何切换成普通用户登录 解决: 重启,同时按Esc建,直至进入到恢复模式下: 选择第一项,进入: 找到ro...那一行,把ro之后的删除,并把ro修改为rw si ...
- 样本稳定指数PSI
信用评定等级划分之后需要对评级的划分做出评价,分析这样的评级划分结果是否具有实用价值,即分析样本分布的稳定程度.样本分布稳定,则信用评定等级划分结果的实用价值就高.采用样本稳定指数( PSI )检验样 ...
- Java 迭代器 工具类
迭代器:Iterator接口 //获取集合中的对象Iterator<E> iterator() interface Iterator { boolean hasNext(); Object ...
- 【RL前沿】深度强化学习的最新进展 by 2017.12.12
作者:Volodymyr Mnih Google DeepMind科学家. 在Geoffrey Hinton的指导下完成了多伦多大学的机器学习博士学位. 在此之前,在Csab Szepesvari的指 ...
- 创建一个多进程(multiprocessing.Process)
进程是资源的集合,每个进程至少包含一个线程 import multiprocessing #导入进程模块import time, threading #导入线程 def thread_run(): p ...
- jdk升级到9,eclipse打不开
jdk从1.8到1.9之后删除了不少之前Deprecated的类. eclipse 版本oxygen和neon对应jdk1.8 eclipse 版本luna和mars对应jdk1.7,1.6 在打开e ...