4-Liner Python Greedy

  • 15
    def findLongestChain(self, pairs):
        cur, res = float('-inf'), 0
        for p in sorted(pairs, key=lambda x: x[1]):
            if cur < p[0]: cur, res = p[1], res + 1
        return res

  • 0

    My solution followed the same idea after got "time limit exceeded" from my initial DP solution, but yours is obviously much clearer and cleaner. Admire!
    lol and found a benefit of using a bit slower language - always force you to find a better solution. If I were using C++ etc maybe the DP solution would pass with no problem and I'll satisfy with that while miss a smarter solution like this!

Log in to reply

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