这道题之前做过
题干
给定一个无序的整数数组,找到其中最长上升子序列的长度。
示例:
输入: [10,9,2,5,3,7,101,18]
输出: 4
解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。
说明:
可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。
你算法的时间复杂度应该为 O(n2) 。
进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗?
想法
参考之前的总结即可
Java代码
package daily;
import java.util.Arrays;
public class lengthOfLIS {
public int lengthOfLIS(int[] nums) {
int[] dp= new int[nums.length];
int res=0;
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[j] + 1);
}
}
res = Math.max(res, dp[i]);
}
return res;
}
public static void main(String args[]){
lengthOfLIS lengthOfLIS=new lengthOfLIS();
int [] nums={10,9,2,5,3,7,101,18};
System.out.println(lengthOfLIS.lengthOfLIS(nums));
}
}
我的leetcode代码
都已经上传到我的git