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 ...
随机推荐
- Android WebView选择本地文件上传
This sample demonstrate android webview choose file to upload. I just implement the client code ,the ...
- Swift中集合类型indexOf(Element)提示错误的解决办法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 初学Swift,会遇到一些潜在的小问题,比如我们在某个集合对象 ...
- 【编程练习】poj1068
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24202 Accepted: 14201 De ...
- Linux系统编程-----进程fork()
在开始之前,我们先来了解一些基本的概念: 1. 程序, 没有在运行的可执行文件 进程, 运行中的程序 2. 进程调度的方法: 按时间片轮转 先来先服务 短时间优先 按优先级别 3. 进程的状态: 就绪 ...
- AndroidVerifyBoot
253 Utils.write(image_with_metadata, outPath);254 }227行得到boot.img的size 238行new一个BootSignat ...
- 概率论:假设检验-t检验和Augmented Dickey–Fuller test
http://blog.csdn.net/pipisorry/article/details/51184556 T检验 T检验,亦称student t检验(Student's t test),学生t检 ...
- 流量控制闸门——LimitLatch套接字连接数限制器
Tomcat作为web服务器,对于每个客户端的请求将给予处理响应,但对于一台机器而言,访问请求的总流量有高峰期且服务器有物理极限,为了保证web服务器不被冲垮我们需要采取一些措施进行保护预防,需要稍微 ...
- Mat, IplImage, CvMat, Cvarr关系及元素获取
自己目前正打算整理opencv数据结构之间关系,寻寻觅觅之间,发现这篇博文很全面,总结得很好,故转之.红色部分不对,自己已修改! 原文地址:http://blog.csdn.net/abcjennif ...
- 【一天一道LeetCode】#121. Best Time to Buy and Sell Stock
# 一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say ...
- Gradle 1.12翻译——第十九章. Gradle 守护进程
有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...