O(N) time, O(1) extra space


  • 0
    B
    public int findLengthOfLCIS(int[] nums) {
                 if (nums == null || nums.length == 0) return 0;
                 final int len = nums.length;
                 if(len == 0) return 0;
                 // if nums is not null and empty, then the max length should be 1 at least...
                 int maxSoFar = 1;
                 int count = 1;
                 for(int i = 1; i < nums.length; i++)
                 {
                     if(nums[i] > nums[i-1])
                     {
                         maxSoFar = Math.max(maxSoFar, ++ count);
                     }
                     else
                     {
                         count = 1;
                     }
                 }
                 return maxSoFar;             
             }
    

Log in to reply
 

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