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 ...
随机推荐
- 【转】PLI是什么以及怎么用
programmable language interface 这里就说给verilog用的一些系统函数,还是无双大大的帖子 首先介绍了怎么让你自己写的pli系统函数在ncverilog里面可以成功调 ...
- 数组中只出现过一次的数字 牛客网 剑指Offer
数组中只出现过一次的数字 牛客网 剑指Offer 题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了偶数次.请写程序找出这两个只出现一次的数字. def FindNumsAppearOnce ...
- Luogu P2822 [NOIp2016提高组]组合数问题 | 数学、二维前缀和
题目链接 思路:组合数就是杨辉三角,那么我们只要构造一个杨辉三角就行了.记得要取模,不然会爆.然后,再用二维前缀和统计各种情况下组合数是k的倍数的方案数.询问时直接O(1)输出即可. #include ...
- 求树的直径【两遍BFS】
两遍BFS.从任意一个点出发,第一遍可以找到直径的一端,从这端出发即可找到另外一端. 证明:从U点出发,到达V[画个图便清晰了] 1.如果U在直径上,则V一定是直径的一个端点. 2.如果U不在直径上. ...
- 第36篇-return字节码指令
方法返回的字节码相关指令如下表所示. 0xac ireturn 从当前方法返回int 0xad lreturn 从当前方法返回long 0xae freturn 从当前方法返回float 0xaf d ...
- 关于iview、element-ui重置表单并清除校验的方法
平时在使用iview或者vue重置表单是时,我会习惯使用 this.$refs[formData].resetFields(); 但是直接这样写上去方法是不起作用的, 内容必须要在每个form-ite ...
- 🔥完美解决ESlint+Prettier各项配置冲突的语法报错问题(新手向)
本文重点: 1.解决修改了Prettier默认配置,项目内格式化无法生效 2.解决Prettier缺少配置,函数名和括号之间,自动添加空格 3.settings.json配置项分享 一个程序员,可能非 ...
- 初步认识express,并创建web服务器,挂载静态资源
1.Express简介 1.1什么是Express 官方给出的概念:Express 是基于 Node.js 平台,快速.开放.极简的 Web 开发框架,官方网址 相似用途:Express 的作用和 N ...
- charles抓包修改请求参数发送新的请求
打开charles -->选择请求右击选择compose---修改参数发送请求
- docker容器运行java后台程序,存到数据库的时间差一天的问题
主要原因是docker容器中的时间用的是标准时间,不是用的宿主机的时间. 修改方法: docker run -e TZ="Asia/Shanghai" -d -p 80:80 -- ...