Java O(nlog(n))


  • 0
    M
    public int findLongestChain(int[][] pairs) {
            Arrays.sort(pairs,new Comparator<int[]>(){
                public int compare(int[] a , int[] b){
                    return a[1]-b[1];
                }
            });
            
            int ret=1;
            int maxtraversed=pairs[0][1];
            for(int i=1;i<pairs.length;i++){
                if(pairs[i][0]>maxtraversed){
                    ret++;
                    maxtraversed=pairs[i][1];
                }
            }
            return ret;
            
        }
    

Log in to reply
 

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