Longest Increasing Subsequence | 2D array | O(n^2)


  • 0
    M
    public class Solution {
        public int findLongestChain(int[][] pairs) {
            
            Arrays.sort(pairs, new Comparator<int[]>() {
                
                public int compare(int[] i1, int[] i2) {
                    return ((Integer)i1[0]).compareTo(i2[0]);
                }
            });
            
            // Arrays.sort(pairs, new ValueComparator());
            
            int[] lis = new int[pairs.length];
            
            for (int i = 0; i < pairs.length; i++) { 
                
                lis[i] = 1;
                
                for (int j = 0; j < i; j++) {
                    
                    if (pairs[i][0] > pairs[j][1]) {
                        lis[i] = Math.max(lis[i], 1 + lis[j]);
                    }
                }
            }
            
            return lis[lis.length - 1];
        }
    }
    

Log in to reply
 

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