LeetCode之“动态规划”:Maximum Subarray
题目要求:
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.
复杂度为O(n)的程序如下:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sz = nums.size();
if(sz == )
return ;
int maxsofar = INT_MIN;
int sum = ;
for(int i = ; i < sz; i++)
{
sum += nums[i];
if(sum > maxsofar)
maxsofar = sum;
if(sum < )
sum = ;
}
return maxsofar;
}
};
我们也可以利用局部最优和全局最优的思想来解决这个问题(参考自一博文):
基本思路是这样的,在每一步,我们维护两个变量,一个是全局最优,就是到当前元素为止最优的解是,一个是局部最优,就是必须包含当前元素的最优的解。接下来说说动态规划的递推式(这是动态规划最重要的步骤,递归式出来了,基本上代码框架也就出来了)。假设我们已知第i步的global[i](全局最优)和local[i](局部最优),那么第i+1步的表达式是:local[i+1]=max(A[i], local[i]+A[i]),就是局部最优是一定要包含当前元素,所以不然就是上一步的局部最优local[i]+当前元素A[i](因为local[i]一定包含第i个元素,所以不违反条件),但是如果local[i]是负的,那么加上他就不如不需要的,所以不然就是直接用A[i];global[i+1]=max(local[i+1],global[i]),有了当前一步的局部最优,那么全局最优就是当前的局部最优或者还是原来的全局最优(所有情况都会被涵盖进来,因为最优的解如果不包含当前元素,那么前面会被维护在全局最优里面,如果包含当前元素,那么就是这个局部最优)。
具体程序如下:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sz = nums.size();
if(sz == )
return ;
vector<int> local(sz, );
vector<int> global(sz, );
local[] = nums[];
global[] = nums[];
for(int i = ; i < sz; i++)
{
local[i] = max(nums[i], nums[i] + local[i - ]);
global[i] = max(global[i - ], local[i]);
}
return global[sz - ];
}
};
这个程序还可以更节省空间:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sz = nums.size();
if(sz == )
return ;
int local = nums[];
int global = nums[];
for(int i = ; i < sz; i++)
{
local = max(nums[i], nums[i] + local);
global = max(global, local);
}
return global;
}
};
LeetCode之“动态规划”:Maximum Subarray的更多相关文章
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- 【LeetCode】053. Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【LeetCode】53. Maximum Subarray (2 solutions)
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...
- LeetCode OJ 53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【一天一道LeetCode】#53. Maximum Subarray
一天一道LeetCode系列 (一)题目 Find the contiguous subarray within an array (containing at least one number) w ...
- (LeetCode 53)Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- Leetcode No.53 Maximum Subarray(c++实现)
1. 题目 1.1 英文题目 Given an integer array nums, find the contiguous subarray (containing at least one nu ...
随机推荐
- OpenMP实现生产者消费者模型
生产者消费者模型已经很古老了吧,最近写了个OpenMP版的此模型之实现,来分享下. 先说一下模型的大致做法是: 1.生产者需要取任务,生产产品. 2.消费者需要取产品,消费产品. 生产者在生产某个产品 ...
- [nginx]查看安装了哪些模块
有时候安装的时候不知道哪些模块忘了安装需要查看下已经安装的模块. 查看安装了哪些模块 $ nginx -V nginx version: nginx/1.4.6 (Ubuntu) built by g ...
- FFmpeg的HEVC解码器源代码简单分析:环路滤波(Loop Filter)
===================================================== HEVC源代码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpe ...
- 随机采样和随机模拟:吉布斯采样Gibbs Sampling
http://blog.csdn.net/pipisorry/article/details/51373090 吉布斯采样算法详解 为什么要用吉布斯采样 通俗解释一下什么是sampling. samp ...
- 初探linux子系统集之timer子系统(三)
因为现在的linux虽然还是可以使用低精度的timer,但是趋势是高精度hrtimer,所以上一篇试着翻译一下hrtimer的一些介绍,翻译的不是很好,看来英语还得好好学习啊,下面还是好好学习下lin ...
- Java子类实例化的过程
//继承 class Work{ public Work(){ System.out.println("父类中的方法"); } } class Worker extends Wor ...
- React Native入门教程 1 -- 开发环境搭建
有人问我为啥很久不更新博客..我只能说在学校宿舍真的没有学习的环境..基本上在宿舍里面很颓废..不过要毕业找工作了,我要渐渐把这个心态调整过来,就从react-native第一篇博客开始.话说RN也出 ...
- (六十四)iOS的socket实现(C+OC混合实现)
对于微博.微信朋友圈之类的网络通信,使用JSON完全可以满足需求,但是如果要制作网络游戏,就需要建立一个持久连接,这时候就要考虑使用socket. 在iOS上实现socket大体有两种方法,一是借助自 ...
- 存储那些事儿(五):BTRFS文件系统之Btree结构详解
Btree数据结构可以说是BTRFS文件系统的基础.它提供了一个通用的方式去存储不同的数据类型.它仅仅存储3个数据类型:key, item和block header. btrfs_header的定义如 ...
- JAVA之旅(十九)——ListIterator列表迭代器,List的三个子类对象,Vector的枚举,LinkedList,ArrayList和LinkedList的小练习
JAVA之旅(十九)--ListIterator列表迭代器,List的三个子类对象,Vector的枚举,LinkedList,ArrayList和LinkedList的小练习 关于数据结构,所讲的知识 ...