Advertisement
ogv

Untitled

ogv
Aug 13th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. /*
  2. Runtime: 1 ms, faster than 92.68% of Java online submissions for Longest Increasing Subsequence.
  3. Memory Usage: 36.8 MB, less than 73.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.         if (nums.length == 1) return 1;
  9.  
  10.         int[] minLast = new int[nums.length];
  11.        
  12.         int lis = 0;
  13.        
  14.         for (int current: nums) {
  15.             int idx = Arrays.binarySearch(minLast, 0, lis, current);
  16.             if (idx >= 0) continue;
  17.            
  18.             int insertionPoint = -idx - 1;
  19.             minLast[insertionPoint] = current;
  20.            
  21.             if (insertionPoint == lis) {
  22.                 ++lis;
  23.             }
  24.         }
  25.        
  26.         return lis;
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement