当前位置:网站首页>LeetCode 300. Daily question of the longest increasing subsequence
LeetCode 300. Daily question of the longest increasing subsequence
2022-07-07 16:58:00 【@Little safflower】
Problem description
Give you an array of integers nums , Find the length of the longest strictly increasing subsequence .
Subsequence Is a sequence derived from an array , Delete ( Or do not delete ) Elements in an array without changing the order of the rest . for example ,[3,6,2,7] It's an array [0,3,1,6,2,2,7] The subsequence .
Example 1:Input :nums = [10,9,2,5,3,7,101,18]
Output :4
explain : The longest increasing subsequence is [2,3,7,101], So the length is 4 .
Example 2:Input :nums = [0,1,0,3,2,3]
Output :4
Example 3:Input :nums = [7,7,7,7,7,7,7]
Output :1
Tips :
1 <= nums.length <= 2500
-104 <= nums[i] <= 104source : Power button (LeetCode)
link :https://leetcode.cn/problems/longest-increasing-subsequence
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
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;
}
}
边栏推荐
猜你喜欢
null == undefined
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
Vs2019 configuration matrix library eigen
字节跳动Android金三银四解析,android面试题app
数据中台落地实施之法
作为Android开发程序员,android高级面试
Master this promotion path and share interview materials
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
【图像传感器】相关双采样CDS
正在准备面试,分享面经
随机推荐
如何选择合适的自动化测试工具?
DNS 系列(一):为什么更新了 DNS 记录不生效?
Introduction and use of gateway
浅浅理解.net core的路由
LeetCode 312. 戳气球 每日一题
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
AutoLISP series (2): function function 2
As an Android Developer programmer, Android advanced interview
字节跳动Android面试,知识点总结+面试题解析
node:504报错
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
QML初学
作为Android开发程序员,android高级面试
[designmode] flyweight pattern
Laravel changed the session from file saving to database saving
Cesium (4): the reason why gltf model is very dark after loading
null == undefined
Sort out several important Android knowledge and advanced Android development interview questions
Vs2019 configuration matrix library eigen
ByteDance Android gold, silver and four analysis, Android interview question app