Skip to content
LC-0646 Medium LeetCode

646. Maximum Length of Pair Chain

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 61% Topics: Array, Dynamic Programming, Greedy, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(1)

class Solution(object):
    def findLongestChain(self, pairs):
        """
        :type pairs: List[List[int]]
        :rtype: int
        """
        pairs.sort(key=lambda x: x[1])
        cnt, i = 0, 0
        for j in xrange(len(pairs)):
            if j == 0 or pairs[i][1] < pairs[j][0]:
                cnt += 1
                i = j
        return cnt

Solution from kamyu104/LeetCode-Solutions · MIT