O(nlogn) Time solution


  • 0
    Q

    class Helper implements Comparator
    {
    public int compare(Object ob1, Object ob2)
    {
    int[] s1= (int[]) ob1;
    int[] s2= (int[]) ob2;
    return s1[1]-s2[1];
    }

    }

    public class Solution {
    public int findLongestChain(int[][] pairs) {
    int n = pairs.length;
    if(n<2)
    return n;
    Arrays.sort(pairs, new Helper());
    int i,count=1;
    int[] temp = pairs[0];
    for(i=1;i<n;i++)
    {
    if(temp[1]<pairs[i][0])
    {
    count++;
    temp=pairs[i];
    }
    }
    return count;
    }
    }


Log in to reply
 

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