2.MaxSubArray-Leetcode
题目:最大连续子序列和
思路:动态规划
状态转移方程
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)的空间复杂度。要求给出伪码。
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的更多相关文章
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode] 题型整理之动态规划
动态规划属于技巧性比较强的题目,如果看到过原题的话,对解题很有帮助 55. Jump Game Given an array of non-negative integers, you are ini ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- leetcode-Maximum Subarray
https://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (contai ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- [LeetCode]题解(python):053-Maximum Subarray
题目来源 https://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (c ...
- LeetCode 刷题记录
写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- BUAA 2020 软件工程 热身作业
BUAA 2020 软件工程 热身作业 Author: 17373051 郭骏 项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 第一次作业-热身! ...
- Noip模拟78 2021.10.16
这次时间分配还是非常合理的,但可惜的是$T4$没开$\textit{long long}$挂了$20$ 但是$Arbiter$上赏了蒟蒻$20$分,就非常不错~~~ T1 F 直接拿暴力水就可以过,数 ...
- S 锁与 X 锁的爱恨情仇《死磕MySQL系列 四》
系列文章 一.原来一条select语句在MySQL是这样执行的<死磕MySQL系列 一> 二.一生挚友redo log.binlog<死磕MySQL系列 二> 三.MySQL强 ...
- Linux 启动/关闭 oracle 数据库
1.启动 1.1 启动监听 :lsnrctl start 1.2 启动数据库:sqlplus /nolog conn /as sysdba(或者两句一起:sqlplus sys/ as sysd ...
- Redis6.2发布 地理位置功能增强了什么?
原文地址:https://developer.aliyun.com/article/780257 Redis社区最近刚刚发布Redis6.2 RC1版本,在本次发布中,阿里云Tair团队(阿里云云内存 ...
- Fiddler抓包工具简介:(四)Fiddler的基本使用
Fiddler的使用 视图功能区域 会话的概念:一次请求和一次响应就是一个会话. fiddler主界面 下面挑几个快捷功能区中常用几项解释,其他功能自己尝试: 快捷功能区 1:给会话添加备注信息 2: ...
- 第三课 Dubbo设计中的设计模式
责任链模式 责任链模式在Dubbo中发挥的作用举足轻重,就像是Dubbo框架的骨架.Dubbo的调用链组织是用责任链模式串连起来的. 责任链中的每个节点实现Filter接口,然后由ProtocolF ...
- [啃书] 第3篇 - 结构体及其操作/浮点数&圆周率/复杂度/测试
啃书部分已单独做成Gitbook了,后续不再更新.详情访问个人网站ccoding.cn或ccbyte.github.io 前言 本篇总结自<算法笔记>2.8-2.10 正文 知识点1:结构 ...
- 新技能GET!在前端表格中花式使用异步函数的奥义
背景 60年代时,操作系统中独立运行的单元通常是进程.但随着计算机技术的发展,人们发现在进程运行过程中,创建.撤销与切换都要花费较大的时空开销. 到了80年代为了解决这一问题,出现了更小的独立运行基本 ...
- Maven 依赖调解源码解析(五):同一个文件内声明,后者覆盖前者
本文是系列文章<Maven 源码解析:依赖调解是如何实现的?>第五篇,主要介绍同一个文件内声明,后者覆盖前者的原则.请按顺序阅读其他系列文章,系列文章总目录参见:https://www.c ...