题目:最大连续子序列和

思路:动态规划

状态转移方程

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. Noip模拟41 2021.8.16

    T1 你相信引力吗 对于区间的大小关系问题,往往使用单调栈来解决 这道题的优弧和劣弧很烦,考虑将其等价的转化 由于所有的合法情况绕过的弧都不会经过最高的冰锥, 又因为环可以任意亲定起点,这样可以直接把 ...

  2. 华为HG255D挂卡中继专用旋风科技固件

    正的挂卡不掉线不掉速,稳定上网看上去好像很NB的样子 挂卡设置教程:http://picimg.lshou.com/pic/clou ... /6/t/1/30247515.mp4 固件链接: htt ...

  3. MiniFly四轴飞行器之部分系统及电源分析

    最近硬件四轴很火,了解了很久,还是选择了MiniFly,主要还是资料多,后边可以有人讨论,不像很多就是建了个群,研究问题还是在论坛方便很多. 四轴终于拿到手,功能很强大,主要是还支持二次开发,可以研究 ...

  4. 零基础入门必备的Linux命令和C语言基础

    文件和目录(底部有视频资料) cd /home 进入 '/ home' 目录' cd - 返回上一级目录 cd -/- 返回上两级目录 cd 进入个人的主目录 cd ~user1 进入个人的主目录 c ...

  5. Spring源码解读(一):Spring的背景起源及框架整体介绍

    一.前言 Spring起源于2002年Rod Johnson写的一本书<Expert One-on-One J2EE>,书里介绍了Java企业应用程序开发情况,并指出Java EE和EJB ...

  6. linux 安装rabbitmq

    1.安装rabbitmq会依赖erlang.socat.unixodbc 下载 unixODBC-2.3.7.tar.gz ,创建路径/usr/local/unixODBC-2.3.7,解压到该路径下 ...

  7. 折腾systemd-nspawn运行centos7

    Archlinux创建Debian/Ubuntu的systemd-nspawn容器是很简单的,因为有debootstrap软件.某天我突然想装个centos7玩玩,搜了半天没发现有什么类似于deboo ...

  8. 庆祝dotnet6,fastgithub送给你

    前言 dotnet6正式发布了,fastgithub是使用dotnet开发的一款github加速器,作为开发者,无人不知github,作为github用户,fastgithub也许是你不可或缺的本机工 ...

  9. 【JAVA】笔记(1)---JVM内存图;方法重载条件;输入方法;转义字符;强制类型转换;变量分类及区别;Java命名规范;

    Java命名规范: 1.包:全部字母小写: 2.类+接口:所有单词的首字母大写: 3.变量+方法:第一个单词的首字母小写,其余单词首字母大写: 3.常量名:所有字母均大写,且用下划线" _ ...

  10. 暑假算法练习Day5

    咕咕了好几天哈哈哈哈,因为这几天在忙一些其他事(bushi ,好吧其实就是自己太懒啦,从今天开始继续每天的算法练习 1010 一元多项式求导 (25 分) 设计函数求一元多项式的导数.(注:\(x^n ...