Only DP!!Easy to be understood!


  • 0
    D

    public class Solution {
    public int lengthOfLIS(int[] nums) {
    if(nums==null||nums.length==0)
    {
    return 0;
    }
    int n=nums.length;
    int[] T=new int[n];
    Arrays.fill(T,1);// since the longest subsequence include the element is at least 1;
    for(int j=1;j<n;j++)//name two pointers,and j from 1 to n to return the maxlength of the of T[n](nuts[n] is included,compare what i point to and what j point to )
    {
    for(int i=0;i<j;i++)
    {
    if(nums[i]<nums[j])
    {
    T[j]=Math.max(T[j],T[i]+1);//get the max include nuts[j]
    }
    else
    {
    continue;
    }
    }

        }
       int  max=T[0];
    

    for(int i=0;i<n;i++)
    {
    max=Math.max(T[i],max);//traverse the T[] to get the max
    }
    return max;}


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.