Java solution, dp, simple


  • 23
    R
    public int lengthOfLIS(int[] nums) {
        if(nums==null || nums.length==0){
            return 0;
        }
        int[] dp = new int[nums.length];
        int max = 1;
        for(int index=0; index<nums.length;index++){
            dp[index]=1;
            for(int dpIndex=0; dpIndex<index; dpIndex++){
                if(nums[dpIndex]<nums[index]){
                    dp[index]=Math.max(dp[index],dp[dpIndex]+1);
                    max=Math.max(dp[index],max);
                }
            }
        }
        return max;
    }

  • 1
    W

    Why do I need to fill dp with 1? Can't I just do dp[0] = 1 before the loop?


  • 0
    I think this code is the most easiest comprehensible!  Thank U!

Log in to reply
 

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