给你一个整数数组 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.com/problems/longest-increasing-subsequence
class Solution {
//dp[i]代表包含[0,i]中包含nums[i]的最长递增子序列
//dp[i]=max(dp[j])+1,其中0≤j<i且num[j]<num[i]
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
int res = 1;
for(int i=0;i<nums.length;i++){
int max = 1;
for(int j=0;j<i;j++){ //去前面判断,哪个比arr[i]数字小
if(nums[j]<nums[i]){
max = Math.max(dp[j]+1,max); //如果小,那么就是dp[j]+1,max取最大值,就是表示可以获取dp[j]的子序列再+上自己
}
}
dp[i] = max;
res = Math.max(res,dp[i]);
}
return res;
}
}