share my java solution,greedy method!


  • 0
    T
    public class Solution {
        public int findLongestChain(int[][] pairs) {
            if(pairs==null || pairs.length<1) return 0;
            if(pairs.length==1) return 1;
            
            Arrays.sort(pairs,new Comparator<int[]>(){
                  public int compare(int[] a,int[] b){
                      return a[1]-b[1];
                  } 
            });
            
            List<int[]> list=new ArrayList<>();
            list.add(pairs[0]);
            for(int i=1;i<pairs.length;i++){
                int[] cur=list.get(list.size()-1);
                if(cur[1]<pairs[i][0]){
                    list.add(pairs[i]);
                }
            }
            return list.size();
        }
    }
    

Log in to reply
 

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