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 ...
随机推荐
- T-SQL注意事项(1)——SET NOCOUNT ON的去与留
前言 用了一段时间T-SQL之后,哪怕自己没用过,也多多少少看过SSMS中的SET NOCOUNT ON命令,很多性能优化文章中都有提到这个东西,它们建议尽可能使用这个命令减少网络传输的压力,那么今天 ...
- UE4读取scv文件 -- 数据驱动游戏性元素
官方文档链接:http://docs.unrealengine.com/latest/CHN/Gameplay/DataDriven/index.html 略懒,稍微麻烦重复的工作,总希望能找人帮忙一 ...
- java多线程的编程实例
java中可有两种方式实现多线程: 一种是继承Thread类: 一种是实现Runnable接口: Thread类 是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的ru ...
- MacOS的菜单状态栏App添加饼型进度
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/52075418 ...
- linux qcom LCD framwork
点击打开链接 0.关键字 MDSS : Multimedia Display sub system DSI: Display Serial Interface 1.涉及文件 (1) drivers\v ...
- 【原创】Nginx+PHP-FPM优化技巧总结
php-fpm的安装很简单,参见PHP(PHP-FPM)手动编译安装.下面主要讨论下如何提高Nginx+Php-fpm的性能. 1.Unix域Socket通信 之前简单介绍过Unix Domain S ...
- Sublime Text 3 使用MarkDown编写带预览的文本
看到别人使用一个叫Markdown的标记语言来完成编码,心里就有点小激动,毕竟简短的几个符号,就可以写出如此精美的界面,实在是让人感到心旷神怡啊.于是我就在网上搜索了一些相关项的设置,于是便有了下面的 ...
- 精通CSS+DIV网页样式与布局--制作实用菜单
在上篇博文中,小编中主要的简单总结了一下CSS中关于如何设置页面和浏览器元素,今天小编继续将来介绍CSS的相关基础知识,这篇博文,小编主要简单的总结一下在CSS中如何制作网页中的菜单,这部分的内容包括 ...
- go: 一个通用log模块的实现
在go里面,虽然有log模块,但是该模块提供的功能并不强,譬如就没有我们常用的level log功能,但是自己实现一个log模块也并不困难. 对于log的level,我们定义如下: const ( L ...
- Opencv2.4.9、VS2010配置及grabcut代码实例
最近opencv库更新到了2.4.9,作为小码农的我紧跟时代的步伐,装了2.4.9这个库.以下是我的配置步骤: 1.从http://opencv.org/上下载opencv2.4.9文件,由于我用的是 ...