Advertisement
1988coder

674. Longest Continuous Increasing Subsequence

Jan 6th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. // LeetCode URL: https://leetcode.com/problems/longest-continuous-increasing-subsequence/
  2.  
  3. /**
  4.  * Sliding Window
  5.  *
  6.  * Time Complexity: O(N)
  7.  *
  8.  * Space Complexity: O(1)
  9.  *
  10.  * N = Length of input nums array.
  11.  */
  12. class Solution {
  13.     public int findLengthOfLCIS(int[] nums) {
  14.         if (nums == null) {
  15.             return 0;
  16.         }
  17.         if (nums.length <= 1) {
  18.             return nums.length;
  19.         }
  20.  
  21.         int maxLenHere = 1;
  22.         int maxLenSoFar = 1;
  23.  
  24.         for (int i = 1; i < nums.length; i++) {
  25.             if (nums[i] > nums[i - 1]) {
  26.                 maxLenHere++;
  27.             } else {
  28.                 maxLenHere = 1;
  29.             }
  30.             maxLenSoFar = Math.max(maxLenSoFar, maxLenHere);
  31.         }
  32.  
  33.         return maxLenSoFar;
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement