PriorityQueue Solution - beats 92%


  • 0
    L
    public int findLongestChain(int[][] pairs) {
            PriorityQueue<int[]> pq = new PriorityQueue<>(pairs.length,new Comparator<int[]>(){
                        public int compare(int[] a,int[] b){
                            return a[1] - b[1];               
                        }     
                
            });
            for(int[] pair:pairs){
                pq.offer(pair);
            }
            int len = 0;
            int lastEnd = Integer.MIN_VALUE;
            while(!pq.isEmpty()){
                int[] p = pq.poll();
                if(lastEnd < p[0]){
                    lastEnd = p[1];
                    len++;
                }
            }
            return len;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.