Could some Python expert help me explain this, please!

  • 0

    I don't understand why my code has TLE although it runs very fast on my computer with the same inputs ([77,82,84,80,398,286,40,136,162], 9794):
    Could someone please double check this:

    class Solution(object):
        def find(self, coins, amount, d):
            if amount in d:
                return d[amount]
            if amount < 0:
                d[amount] = -1
            elif amount == 0:
                d[amount] = 0
                r = 10000
                for coin in coins:
                    result = self.find(coins, amount - coin, d)
                    if result != -1:
                        r = min(r, result + 1)
                if r == 10000:
                    d[amount] = -1
                    d[amount] = r
            return d[amount]
        def coinChange(self, coins, amount):
            return self.find(sorted(coins, reverse=True), amount, {})

  • 0

    When TLE happened and a test case is given, it only means that the TLE happened while executing that test case.
    It's possible that your solution took too much time during previous test cases and this case is just the last straw.

  • 0

    @dasheng2 Makes sense. Thank you!

Log in to reply

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