Why is my code timing out?


  • 0
    P
    class Solution(object):
        def coinChange(self, coins, amount):
            """
            :type coins: List[int]
            :type amount: int
            :rtype: int
            """
            if amount == 0:
                return 0
            memo = [-1]*(amount+1)
            memo[0] = 0
            for i in range(0,amount+1):
                temp = []
                for j in range(0,len(coins)):
                    if(i >= coins[j]):
                        if(memo[i] > 0):
                            memo[i]= min(memo[i],memo[i-coins[j]]+1)
                        else:
                            memo[i] = memo[i-coins[j]]+1
               
            
            if memo[amount] == 0:
                return -1
            else:
                return memo[amount]

Log in to reply
 

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