ogv

Untitled

ogv
Aug 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. /*
  2. Runtime: 3 ms, faster than 68.76% of Java online submissions for Longest Increasing Subsequence.
  3. Memory Usage: 36.8 MB, less than 70.00% of Java online submissions for Longest Increasing Subsequence.
  4. */
  5. class Solution {
  6.     public int lengthOfLIS(int[] nums) {
  7.         if (nums.length == 0) return 0;
  8.                
  9.         int[] minLast = new int[nums.length + 1];        
  10.        
  11.         int lis = 1;
  12.         minLast[0] = nums[0];
  13.                          
  14.         for (int i = 1; i < nums.length; i++) {
  15.             int current = nums[i];
  16.            
  17.             minLast[0] = Math.min(minLast[0], current);                    
  18.            
  19.             for (int j = 1; j < lis; j++) {                
  20.                 if (current > minLast[j - 1]) {
  21.                     minLast[j] = Math.min(minLast[j], current);                    
  22.                 }
  23.             }
  24.            
  25.             if (current > minLast[lis - 1]) {
  26.                 minLast[lis++] = current;                
  27.             }
  28.         }
  29.        
  30.         return lis;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment