当前位置:网站首页>动规初解||最大子序和 最长上升子序列53、128
动规初解||最大子序和 最长上升子序列53、128
2022-04-22 14:12:00 【借点头发吧】
53. 最大子序和
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
示例:
输入: [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
题解:
1、贪心
ans为数组首元素,sum初始化为0,遍历nums,若sum>0保留,并更新(对子序列和有增益效果),若sum<=0(对子序列和无增益效果),舍弃并且令其为当前遍历到的nums[i]
- ans=-2 sum=0 ,ans=0
- sum>0(n) sum=num=1 ans=1
- sum>0(y) sum=-2 ans=1
- sum>0(n) sum=num=4 ans=4
- sum>0(y) sum=3 ans=4
- sum>0(y) sum=5 ans=5
- sum>0(y) sum=6 ans=6
- sum>0(y) sum=1 ans=6
…
2、动规
dp[i]表示nums中以nums[i]结尾的最大子序和
dp[i]=max(dp[i-1]+nums[i],nums[j])
dp[i]时当前数字要么是与之前的最大子序和的和
时间复杂度O(n),空间复杂度O(n)
优化方法:只与前一项有关所以将dp数组换为int保存前一个的信息,优化后时间复杂度为O(1)。
solution:
1、贪心
class Solution {
public int maxSubArray(int[] nums) {
int ans = nums[0];
int sum = 0;
for(int num: nums) {
if(sum > 0) {
sum += num;
} else {
sum = num;
}
ans = Math.max(ans, sum);
}
return ans;
}
}
300. 最长上升子序列
给定一个无序的整数数组,找到其中最长上升子序列的长度。
示例:
输入: [10,9,2,5,3,7,101,18]
输出: 4
解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。
说明:
可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。
你算法的时间复杂度应该为 O(n2) 。
进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗?
题解:
1、动规
dp[i]是以nums[i]这个数结尾的最长递增子序列的长度
dp[i]=Math.max(dp[i],dp[j]+1)
Array.fill(dp,1) //dp数组全部初始化为1!
子序列:不要求连续子序列
sloution:
class Solution {
public int longestConsecutive(int[] nums) {
int[] dp=new int[nums.length];
Arrays.fill(dp,1);
for(int i=0;i<nums.length;i++){
for(int j=0;j<i;j++){
if(nums[j]<nums[i])
dp[i]=Math.max(dp[i],dp[i]+1);
}
}
int res=0;
for(int i=0;i<dp.length;i++){
res=Math.max(res,dp[i]);
}
return res;
}
}
版权声明
本文为[借点头发吧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_47866171/article/details/108968576
边栏推荐
- Redis connection tool cannot connect to redis in docker
- ICLR2022杰出论文奖出炉,清华、人大获奖,浙大提名
- P2b paper reproduction - point cloud learning record
- 万元礼品奖池!玩转「Lighthouse」有奖征文来袭
- LeetCode——字符的最短距离
- LeetCode-3 无重复字符的最长子串
- "Enterprise service" of digital operation and management of Industrial Park
- Method of page nesting
- 【论文笔记】Vision Transformers for Dense Prediction
- A solution to the problem of buying and selling stocks by force deduction
猜你喜欢

"Enterprise service" of digital operation and management of Industrial Park

万元礼品奖池!玩转「Lighthouse」有奖征文来袭

Timer--

2D conversion (move: translate, rotate: rotate, scale: scale, 2D conversion synthesis)

二月份,我靠这一份PDF文档面试BAT,没想到竟然收到了5个offer

Solution to the blank page of small and medium-sized programs running from uniapp to wechat developer tool

获取数据库中数值时,数据库有值,却为空??

嵌入式软件bug从哪来,怎么去

2D转换(移动:translate,旋转:rotate,缩放:scale,2D转换综合写法)

Lors de l'obtention d'une valeur dans la base de données, la base de données a une valeur, mais elle est vide.
随机推荐
多线程初阶
关于半导体的8个奇特事实
多线程进阶
Is it effective to control caching through meta information in HTML files? Is it used much at present?
Special topic of game partners: breederdao reaches a new height in cooperation with fancy birds
When getting the value in the database, the database has a value, but it is empty??
2D conversion (move: translate, rotate: rotate, scale: scale, 2D conversion synthesis)
Osgearth configuring Map Resources
Redis connection tool cannot connect to redis in docker
Golang:包管理
985 official announcement: international ranking is no longer a construction goal!
Redis相比memcached
Live classroom system platform software source code available for personal testing
Seven years of "one sword" and standing at the edge of the cloud, how to accelerate the digital transformation of enterprises?
How to use openfeign to call the third-party interface
Blocking mode and non blocking mode of socket
[summary of Kunpeng migration and practice Posts] the first play~~
P2B论文复现——点云学习记录
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
字节跳动的面试分享,为了拿下这个offer鬼知道我经历了什么