Advertisement
Guest User

Grokking #190

a guest
Sep 26th, 2021
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. class Solution {
  2.   public int lengthOfLIS(int[] nums) {
  3.     ArrayList<Integer> sub = new ArrayList<>();
  4.     sub.add(nums[0]);
  5.  
  6.     for (int i = 1; i < nums.length; i ++) {
  7.       int num = nums[i];
  8.  
  9.       if (num > sub.get(sub.size() - 1)) {
  10.         sub.add(num);
  11.       } else {
  12.         // Replace the first element that is greater than or equal to num in sub
  13.         int j = 0;
  14.         while (num > sub.get(j)) {
  15.           j += 1;
  16.         }
  17.  
  18.         sub.set(j, num);
  19.       }
  20.     }
  21.  
  22.     return sub.size();
  23.   }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement