![image.png](/uploads/projects/u213900@nwkz2s/f1e9165eed070da4d71d0ccc923655ba.png)
解决思路
动态规划
![image.png](/uploads/projects/u213900@nwkz2s/f5babb4b92e8a5f7995ae45c153cca6c.png)
初始情况下每个元素的LIS值
![image.png](/uploads/projects/u213900@nwkz2s/bc9d864155de47a84827f77e8da584e1.png)
最后每个元素的LIS值
code
class Solution {
public int lengthOfLIS(int[] nums) {
if(nums.length==0)
return 0;
//memo[i]表示以nums[i]为结尾的最长上升子序列的长度
int[] memo = new int[nums.length];
Arrays.fill(memo,1);
for(int i=1;i<nums.length;i++){
for(int j=0;j<i;j++){
if(nums[j]<nums[i])
memo[i] = Math.max(memo[i],1+memo[j]);
}
}
int res = 1;
for(int i:memo)
res = Math.max(res,i);
return res;
}
}
动态规划+二分查找
![image.png](/uploads/projects/u213900@nwkz2s/569473ce188005e4128e4917e7fe5c49.png)
![image.png](/uploads/projects/u213900@nwkz2s/2c45168587cc95bed46b2deb0cf6c067.png)
class Solution {
public int lengthOfLIS(int[] nums) {
int[] tails = new int[nums.length];
int res = 0;
for(int num : nums) {
int i = 0, j = res;
while(i < j) {
int m = (i + j) / 2;
if(tails[m] < num) i = m + 1;
else j = m;
}
tails[i] = num;
if(res == j) res++;
}
return res;
}
}