top of page
Search

Implementation of Patience Sort | Longest Increasing Subsequence in O(n log n) time

Writer's picture: Coding CampCoding Camp


Solution:


class Solution {
    public int lengthOfLIS(int[] nums) {
        TreeSet<Integer> set = new TreeSet<>();
        for(int i=0;i<nums.length;i++)
        {
            Integer x=set.ceiling(nums[i]);
            if(x!=null)
            {
                set.remove(x);
            }
            set.add(nums[i]);
        }
        return set.size();
    }
}

 
 
 

Recent Posts

See All

Comments


bottom of page