当前位置:网站首页>LeetCode 300. 最长递增子序列 每日一题
LeetCode 300. 最长递增子序列 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。
子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。
示例 1:输入:nums = [10,9,2,5,3,7,101,18]
输出:4
解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。
示例 2:输入:nums = [0,1,0,3,2,3]
输出:4
示例 3:输入:nums = [7,7,7,7,7,7,7]
输出:1
提示:
1 <= nums.length <= 2500
-104 <= nums[i] <= 104来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-increasing-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Java
class Solution {
public int lengthOfLIS(int[] nums) {
int n = nums.length;
if(n == 1) return 1;
int[] dp = new int[n];
dp[0] = 1;
int ans = 0;
for(int i = 1;i < n;i++){
dp[i] = 1;
for(int j = 0;j < i;j++){
if(nums[j] < nums[i]){
dp[i] = Math.max(dp[i],dp[j] + 1);
}
}
ans = Math.max(ans,dp[i]);
}
return ans;
}
}
边栏推荐
猜你喜欢
随机推荐
Ray and OBB intersection detection
Detailed explanation of several ideas for implementing timed tasks in PHP
LeetCode 1031. 两个非重叠子数组的最大和 每日一题
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Read PG in data warehouse in one article_ stat
Introduction and use of gateway
AutoLISP series (1): function function 1
面向接口编程
【C 语言】 题集 of Ⅹ
Direct dry goods, 100% praise
Pisa-Proxy SQL 解析之 Lex & Yacc
【DesignMode】享元模式(Flyweight Pattern)
【Android -- 数据存储】使用 SQLite 存储数据
Master this promotion path and share interview materials
应用在温度检测仪中的温度传感芯片
全网“追杀”钟薛高
Personal notes of graphics (3)
OpenGL personal notes
LeetCode 1186. 删除一次得到子数组最大和 每日一题
网关Gateway的介绍与使用