JS DP


  • 0
    C

    JS solution

    /**
     * @param {number[][]} pairs
     * @return {number}
     */
    var findLongestChain = function (pairs) {
      var n = pairs.length;
      var cmp = function (a, b) {
        return a[0]-b[0]
      }
      var _pairs = [].sort.call(pairs, cmp)
      
      var mcl = []
      for (var i = 0; i < n; i++) {
        mcl[i] = 1;
      }
    
      for (var i = 1; i < n; i++) {
        for (var j = 0; j < n; j++) {
          if(_pairs[i][0] > _pairs[j][1] && mcl[i] < mcl[j] + 1){
            mcl[i] += 1;
          }
        }
      }
      return Math.max.apply(null,mcl)
    }
    

Log in to reply
 

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