给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。
子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。
1、普通动态规划
class Solution {
public int lengthOfLIS(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[i] > nums[j]){
dp[i] = Math.max(dp[j] + 1,dp[i]);
}
}
}
int res = 0;
for(int i = 0; i < dp.length;i++){
res = Math.max(res,dp[i]);
}
return res;
}
}
执行用时:55 ms, 在所有 Java 提交中击败了71.00%的用户
内存消耗:40.7 MB, 在所有 Java 提交中击败了64.42%的用户