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 ...
随机推荐
- CentOS6.7下安装MySQL
第一步:到MySQL官网上下载linux版本的MySQL rpm 安装包. 第二步: 将该压塑包解压后,有如下文件: 第三步:安装文件,我们需要安装的文件有 MySQL-server-5.6.26-1 ...
- UNIX环境高级编程——实现uid to name
setpwent()用来将getpwent()的读写地址指回文件开头,即从头读取密码文件中的账号数据. strcut passwd * getpwent(void); getpwent()用来从密码文 ...
- 你不可不知的Eclipse快捷键
我们都知道Eclipse是一个深受广大程序员喜爱的编译器,其插件机制更是让人拜服.它之所以这么被人喜爱,除了这些,最重要的是它丰富的快捷键.那么今天,我就来分享一下我平时经常使用的一些快捷键. Ctr ...
- 06 Activity OnNewIntent方法
OnNewIntent方法:该方法体现在Activity的启动模式上 如sigleTop上: X这个Activity启动模式为sigleTop,Y这个Activity启动模式为stdanderd 那么 ...
- Linux上程序调试的基石(1)--ptrace
引子: 1.在Linux系统中,进程状态除了我们所熟知的TASK_RUNNING,TASK_INTERRUPTIBLE,TASK_STOPPED等,还有一个TASK_TRACED.这表明这个进程处于什 ...
- 使用Hash函数和MAC产生伪随机数
基于Hash函数的PRNG 流程非常类似于对称密码的CTR工作模式 算法的伪码如下 m = ⌈n/outlen⌉ data = V W = the null String for i = 1 to m ...
- Java中怎么简单的使用正则表达式?
对于正则表达式,我通常的认识就是通过一些陌生的奇怪的符号就可以完成很复杂事件的好帮手!实际上正则表达式确实是这方面的好助手,接下来让我们一起认识一下Java中怎么使用正则表达式吧. 初见Pattern ...
- 最简单的基于FFmpeg的内存读写的例子:内存转码器
===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...
- android dataBinding详解
官方介绍地址:http://developer.android.com/intl/zh-cn/tools/data-binding/guide.html 2015 Google IO 大会带来的 Da ...
- UNIX环境高级编程——无名管道和有名管道
一.进程间通信 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区,进程2 ...