题目:最大连续子序列和

思路:动态规划

状态转移方程

f[j]=max{f[j-1]+s[j],s[j]}, 其中1<=j<=n

target = max{f[j]}, 其中1<=j<=n

class Solution {
public:
int maxSubArray(vector<int>& nums) {
if(nums.size()==0)return -1;
if(nums.size()==1)return nums[0];
vector<int> res_vec(nums.size());
res_vec[0]=nums[0];
int max_val = nums[0];
for(vector<int>::size_type i=1;i<nums.size();++i)
{
res_vec[i]=max(res_vec[i-1]+nums[i],nums[i]);
if(max_val<res_vec[i])max_val = res_vec[i];
}
return max_val;
}
};

下面给出一个类似的题:

给定一个整数的数组,相邻的数不能同时选,求从该数组选取若干整数,使得他们的和最大,要求只能使用o(1)的空间复杂度。要求给出伪码。

定义f(n)为以A[n]结尾的序列最大和
f(n)=max(f(n−1),max(f(n−2)+A[n],A[n])
int getMax(int a[],int len)
{
int max1 = a[0];//表示maxSum(n-2);
int max2 = a[0]>a[1]? a[0]:a[1]; //表示maxSum(n-1);
int max3 = 0; // n
for(int i =2; i<len; i++){
max3 = Max(a[i],Max(max1+a[i],max2));
max1 = max2;
max2 = max3;
}
return max3;
}

2.MaxSubArray-Leetcode的更多相关文章

  1. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  2. [leetcode] 题型整理之动态规划

    动态规划属于技巧性比较强的题目,如果看到过原题的话,对解题很有帮助 55. Jump Game Given an array of non-negative integers, you are ini ...

  3. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  4. leetcode-Maximum Subarray

    https://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (contai ...

  5. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  6. [LeetCode]题解(python):053-Maximum Subarray

    题目来源 https://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (c ...

  7. LeetCode 刷题记录

    写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...

  8. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  10. LeetCode 53. Maximum Subarray(最大的子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. 进阶区forgotg攻防世界

    攻防世界进阶区--forgot 前言,这题中看不中用啊宝友!!! 1.查看保护 第一反应就是蛮简单的,32位. 2.获取信息(先运行程序看看) 装的可以,蛮多的东西. 但是就是中看不中用 3.ida ...

  2. Python爬虫之爬取淘女郎照片示例详解

    这篇文章主要介绍了Python爬虫之爬取淘女郎照片示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 本篇目标 抓取淘宝MM ...

  3. 六个好习惯让你的PCB设计更优

    PCB layout工程师每天对着板子成千上万条走线,各种各样的封装,重复着拉线的工作,也许很多人会觉得是很枯燥无聊的工作内容.看似软件操作搬运工,其实设计人员在过程中要在各种设计规则之间做取舍,兼顾 ...

  4. 实验 1: SDN拓扑实践

    (图片和文档是自己写的,因为在CSDN也写了,所以会有自己的水印) 一.实验目的 能够使用源码安装Mininet: 能够使用Mininet的可视化工具生成拓扑: 能够使用Mininet的命令行生成特定 ...

  5. Shooting Bricks题解

    题目传送门 以后我绝对不会一直磕着一道题磕几个小时了...感觉还是自己节奏出了问题,不知为啥感觉有点小慌... 算了,其实再回头仔细看一下这个题dp的思路还是比较好想出来的,打代码之前一定要做好足够的 ...

  6. ASP.NET Core设置URLs的几种方法

    前言 在使用ASP.NET Core 3.1开发时,需要配置服务器监听的端口和协议,官方帮助文档进行简单说明,文档中提到了4种指定URL的方法 设置ASPNETCORE_URLS 环境变量: 使用do ...

  7. Java测试开发--sts安装Lombok(七)

    1.sts安装Lombok的步骤: 下载最新的lombok.jar包,进入cmd窗口,切到Lombok下载的目录,运行命令: java -jar lombok.jar,会出现如下界面: 已经默认选好了 ...

  8. idea Mark Directory as 的几种文件类型

    1. Source roots (or source folders) 源文件夹 通过为该类别分配文件夹,可以告诉IntelliJ IDEA该文件夹及其子文件夹包含应在构建过程中进行编译的源代码. 2 ...

  9. [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <Evaluate> at src/views/index/

    关于vue报错: [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly ...

  10. Emmet快速语法—助力HTML/CSS一行代码一个页面

    学会之后牛掰的场景如下 我们的目标就是用一行代码=>写下面这样的长长长长的HTML结构来. 如:table>(thead.text>th{手机1}*4)+(tbody.text$*4 ...