top of page
Search
Writer's pictureCoding Camp

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



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();
    }
}

135 views0 comments

Recent Posts

See All

Smallest String With A Given Numeric Value

The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the...

Comments


bottom of page