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 ...
随机推荐
- logstash的mutate过滤器的使用
logstash的mutate过滤器的使用 一.背景 二.需求 三.实现步骤 1.安装 `csv codec` 插件 2.准备需要读取的文件数据 3.编写 pipeline ,读取和输出数据 4.mu ...
- EFCore_环境搭建与简单使用_01
开发环境搭建 经典步骤:建实体类.建DbContext.生成数据库 本次使用codefirst模式,走下流程,(你也可以先建好数据库,用命令行的形式,直接生成DbContext,而且生成的DbCont ...
- 并发编程从零开始(十一)-Atomic类
并发编程从零开始(十一)-Atomic类 7 Atomic类 7.1 AtomicInteger和AtomicLong 如下面代码所示,对于一个整数的加减操作,要保证线程安全,需要加锁,也就是加syn ...
- AtCoder Beginner Contest 210题解
A B 过水,略... C 统计长度为k的区间的最多本质不同的数.用尺取法维护下左右指针就可以了.调了许久的原因是更新答案时出现了问题. 当我移动指针时,我们应该移动一个就更新一个,而不是将移动与更新 ...
- python解释器的下载与安装
python解释器 1. 什么是python解释器 用一种能让电脑听的懂得语言,使得电脑可以听从人们的指令去进行工作(翻译官) Python解释器本身也是个程序, 它是解释执行Python代码的,所以 ...
- IDM使用教程:利用IDM下载百度网盘文件
IDM是什么 其实我使用IDM下载器只是为了方便网页版百度网盘直接下载大于40M文件而已,大家知道文件过大必须打开客户端才能下载,这点对于我的破电脑感觉很烦躁,每次要等待它慢悠悠打开,然后动用我的超级 ...
- 好好的 Tair 排行榜不用,非得自己写?20 行代码实现高性能排行榜
TairZset 是阿里云自研的可实现任意维度 double 类型的分值排序的数据结构,借助 Tair 客户端同时可实现扩展性,即可以将计算任务分布至多个数据节点完成,实现分布式排行榜能力.本文介绍了 ...
- MSSQL SQL注入 总结
0x00 MSSQL 基础 MSSQL系统自带库和表 系统自带库 MSSQL安装后默认带了6个数据库,其中4个系统级库:master,model,tempdb和msdb:2个示例库:Northwind ...
- webpack 打包图片资源
webpack 打包图片资源 /** * loader: 1. 下载 2. 使用(配置) * plugins:1. 下载 2. 引入 3.使用 */ // 用来拼接绝对路径的方法 const {res ...
- 问题 B: 比大小
题目描述 给你两个很大的数,你能不能判断出他们两个数的大小呢? 比如123456789123456789要大于-123456 输入 每组测试数据占一行,输入两个不超过1000位的10进制整数a,b 数 ...