Concise Java Solution, DP, On space


  • 2
    J
    public class Solution {
    public int lengthOfLIS(int[] nums) {
        if(nums == null || nums.length == 0) {
            return 0;
        }
        int[] res = new int[nums.length];
        int max = 0;
        for(int i = 0; i < nums.length; i++) {
            res[i] = 1;
            for(int j = 0; j < i; j++) {
                if(nums[j] < nums[i]) {
                    res[i] = (res[i] > res[j]) ? res[i] : (res[j] + 1);
                }
            }
            max = Math.max(max, res[i]);
        }
        return max;
    }
    

    }


Log in to reply
 

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