My simple accepted solution in Java


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

Log in to reply
 

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