why run time exceeds if separate the equal conditions when using python

  • 0

    At first my solution like this:

    import sys
    class Solution(object):
        def coinChange(self, coins, amount):
            :type coins: List[int]
            :type amount: int
            :rtype: int
            MAX = float("inf")
            a = [0] + [MAX for i in range(amount)]
            for am in xrange(1, amount + 1):
                for coin in coins:
                    if coin == am: #there add equal condition,if equal then set the value to 1 and break
                        a[am] = 1
                    if coin < am:
                        a[am] = min(a[am] - 1, a[am - coin]) + 1
            if a[amount] == MAX:
                return -1
            return a[amount]

    but the time exceeds,and after I combine lower an equal condition to an if statement,it passed.So I'm confused why my "optimization" slows the running speed at contrast?Anyone can tell me the reason,thanks!

Log in to reply

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