description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
self.res = float("inf")
n = len(coins)
coins.sort(reverse=True)
def helper(cur, start, cnt, n):
if cur == amount:
self.res = min(self.res, cnt)
if cur > amount:
return
for i in range(start, n):
if (
cur + coins[i] <= amount
and cur + coins[i] * (self.res - cnt) > amount
):
helper(cur + coins[i], i, cnt + 1, n)
helper(0, 0, 0, n)
return self.res if self.res < float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
@lru_cache(maxsize=None)
def helper(amount):
if amount <= 0:
return 0
min_so_far = math.inf
for i in range(len(coins)):
if coins[i] <= amount:
min_so_far = min(min_so_far, 1 + helper(amount - coins[i]))
return min_so_far
result = helper(amount)
if result == math.inf:
return -1
return result | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
sz = len(coins) + 1
dp = [([sys.maxsize] * sz) for _ in range(amount + 1)]
for i in range(sz):
dp[0][i] = 0
for a in range(1, amount + 1):
for i in range(1, sz):
dp[a][i] = dp[a][i - 1]
if a - coins[i - 1] >= 0:
dp[a][i] = min(1 + dp[a - coins[i - 1]][i], dp[a][i])
return -1 if dp[amount][sz - 1] == sys.maxsize else dp[amount][sz - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
cache = {}
def coinChange(self, coins: List[int], amount: int) -> int:
self.cache = {}
return self.solve(coins, amount)
def solve(self, coins, amount):
if amount == 0:
return 0
if amount < 0:
return -1
if amount in self.cache:
return self.cache[amount]
best = -1
for c in coins:
add = self.solve(coins, amount - c)
if add != -1:
if best == -1:
best = add + 1
else:
best = min(best, add + 1)
self.cache[amount] = best
return best | CLASS_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
states = [0] * (amount + 1)
for s in range(0, amount + 1):
if s == 0:
states[s] = 0
else:
min_c = 1000000000.0
for c in coins:
if c <= s and states[s - c] != -1:
min_c = min(min_c, 1 + states[s - c])
if min_c == 1000000000.0:
states[s] = -1
else:
states[s] = min_c
print(s, states[s])
return states[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not amount:
return 0
coins.sort()
visited = {}
sums = []
for coin in coins:
if coin == amount:
return 1
visited[coin] = 1
sums.append(coin)
q = []
q.append((1, sums))
while q:
count, sums = q.pop()
next_sums = []
for coin in coins:
for s in sums:
current = coin + s
if current < amount and current not in visited:
visited[current] = 1
next_sums.append(current)
elif current == amount:
return count + 1
else:
visited[current] = 1
if next_sums:
q.insert(0, (count + 1, next_sums))
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
rs = [amount + 1] * (amount + 1)
rs[0] = 0
for i in range(1, amount + 1):
for c in coins:
if i >= c:
rs[i] = min(rs[i], rs[i - c] + 1)
if rs[amount] == amount + 1:
return -1
return rs[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0] + [float("inf")] * amount
coinValues = collections.defaultdict(list)
for coin in coins:
for i in range(coin, amount + 1):
if dp[i - coin] + 1 < dp[i]:
dp[i] = dp[i - coin] + 1
coinValues[i] = coinValues[i - coin] + [coin]
print(coinValues[amount])
return dp[-1] if dp[-1] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChangeDynamic(
self, coins: List[int], amount: int, memory_dict: dict
) -> int:
if amount < 0:
return -1
elif amount == 0:
return 0
elif memory_dict.get(amount) is None:
recursive_call_output = []
for coin_val in coins:
recursive_call_output.append(
self.coinChangeDynamic(coins, amount - coin_val, memory_dict)
)
min_num_coins = float("inf")
for num_coins in recursive_call_output:
if num_coins >= 0 and num_coins < min_num_coins:
min_num_coins = num_coins
if min_num_coins == float("inf"):
memory_dict[amount] = -1
return -1
else:
memory_dict[amount] = min_num_coins + 1
return min_num_coins + 1
else:
return memory_dict[amount]
def coinChange(self, coins: List[int], amount: int) -> int:
return self.coinChangeDynamic(coins, amount, {}) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR DICT VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0 or not coins:
return 0
minlst = [float("inf")] * (amount + 1)
n = len(minlst)
minlst[0] = 0
for i in range(1, n):
for denom in coins:
remainder = i - denom
if remainder < 0:
continue
newsmallest = min(minlst[remainder] + 1, minlst[i])
minlst[i] = newsmallest
return -1 if minlst[-1] == float("inf") else minlst[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not coins or amount < 0:
return -1
elif amount == 0:
return 0
coins.sort(reverse=True)
visited = set()
q = collections.deque([])
for c in coins:
if c == amount:
return 1
elif c < amount:
q.append(c)
visited.add(c)
count = 1
while q:
size = len(q)
count += 1
for _ in range(size):
prev = q.popleft()
for c in coins:
cur = prev + c
if cur == amount:
return count
elif cur < amount and cur not in visited:
visited.add(cur)
q.append(cur)
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount < 1:
return 0
self.cache = [0] * (amount + 1)
return self.coin_change(coins, amount)
def coin_change(self, coins, remainder):
if remainder < 0:
return -1
if remainder == 0:
return 0
if self.cache[remainder] != 0:
return self.cache[remainder]
system_max = sys.maxsize
minimum = system_max
for coin in coins:
change_result = self.coin_change(coins, remainder - coin)
if change_result >= 0 and change_result < minimum:
minimum = 1 + change_result
self.cache[remainder] = -1 if minimum == system_max else minimum
return self.cache[remainder] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER VAR RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort()
if amount == 0:
return 0
changes = [amount + 1] * (amount + 1)
changes[0] = 0
for i in range(1, amount + 1):
for coin in coins:
if coin > i:
break
else:
changes[i] = min(changes[i], changes[i - coin] + 1)
if changes[-1] != amount + 1:
return changes[-1]
else:
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER RETURN VAR NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | INF = 100000000000000000
def hashIntArr(i_arr):
return "-".join(list(map(str, i_arr)))
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins = sorted(coins)
mc = [
[(0 if j == 0 else INF) for j in range(amount + 1)]
for _ in range(len(coins) + 1)
]
for c in range(1, len(coins) + 1):
coin_value = coins[c - 1]
for a in range(1, amount + 1):
if coin_value <= a:
mc[c][a] = min(1 + mc[c][a - coin_value], mc[c - 1][a])
else:
mc[c][a] = mc[c - 1][a]
min_coins = mc[len(coins)][amount]
return min_coins if min_coins < INF else -1 | ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
d = [0] + [float("inf")] * amount
for v in range(1, len(d)):
for c in coins:
if c <= v and d[v] > d[v - c] + 1:
d[v] = d[v - c] + 1
return d[-1] if d[-1] < float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
T = [float("inf")] * (amount + 1)
T[0] = 0
for amnt in range(1, amount + 1):
curr_min = float("inf")
for k in range(len(coins)):
if coins[k] <= amnt:
val = 1 + T[amnt - coins[k]]
if val < curr_min:
curr_min = val
T[amnt] = curr_min
return -1 if T[-1] == float("inf") else T[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
rows = amount + 1
cols = len(coins) + 1
cache = [[(0) for _ in range(cols)] for _ in range(rows)]
for row in range(1, rows):
cache[row][0] = -1
for row in range(1, rows):
for col in range(1, cols):
newAmt = row - coins[col - 1]
takeValue = (
cache[newAmt][col] if newAmt >= 0 and newAmt < len(cache) else -1
)
takeCoin = 1 + takeValue if takeValue >= 0 else -1
skipCoin = cache[row][col - 1]
if skipCoin < 0:
cache[row][col] = takeCoin
elif takeCoin < 0:
cache[row][col] = skipCoin
else:
cache[row][col] = min(skipCoin, takeCoin)
return cache[amount][len(coins)] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_CALL VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
value = [-1] * (amount + 1)
value[0] = 0
for j in range(amount + 1):
for i in range(len(coins)):
if j > coins[i]:
if value[j - coins[i]] != -1:
if value[j] == -1:
value[j] = value[j - coins[i]] + 1
else:
value[j] = min(value[j], value[j - coins[i]] + 1)
if j == coins[i]:
value[j] = 1
print(value)
return value[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
cache = {(0): 0}
def recurse(i):
if i in cache:
return cache[i]
n = i + 1
for coin in coins:
curr = 0
if i >= coin:
next_amount = recurse(i - coin)
if next_amount >= 0:
curr = 1 + next_amount
if curr > 0:
n = min(n, curr)
result = -1 if n == i + 1 else n
cache[i] = result
return result
return recurse(amount) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
memory = {}
def numCoins(self, coins, amount):
if amount in self.memory:
return self.memory[amount]
nc = float("inf")
for c in coins:
if amount >= c:
nnc = self.numCoins(coins, amount - c)
nc = min(nc, nnc + 1)
self.memory[amount] = nc
return nc
def coinChange(self, coins: List[int], amount: int) -> int:
self.memory = {}
self.memory[0] = 0
for c in coins:
self.memory[c] = 1
nc = self.numCoins(coins, amount)
if nc == float("inf"):
return -1
return nc | CLASS_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
numWays = [float("inf")] * (amount + 1)
numWays[0] = 0
for coin in coins:
for value in range(coin, amount + 1):
numWays[value] = min(numWays[value], 1 + numWays[value - coin])
print(numWays[amount])
return numWays[amount] if numWays[amount] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def bfs(self, cur, coins):
if cur == 0:
return 0
queue = [(0, cur)]
visited = {}
while queue:
times, cur = heapq.heappop(queue)
for c in coins:
if c == cur:
return times + 1
if c < cur:
if cur - c not in visited:
heapq.heappush(queue, (times + 1, cur - c))
visited[cur - c] = True
return -1
def coinChange(self, coins: List[int], amount: int) -> int:
return self.bfs(amount, coins) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER VAR ASSIGN VAR DICT WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
memo = [float("inf") for _ in range(amount + 1)]
memo[0] = 0
for i in range(1, len(memo)):
minv = float("inf")
for coin in coins:
if i - coin >= 0 and memo[i - coin] < minv:
minv = memo[i - coin]
memo[i] = 1 + minv
return memo[amount] if memo[amount] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [amount + 1] * (amount + 1)
dp[0] = 0
for i in range(len(coins)):
coin = coins[i]
for j in range(1, amount + 1):
sameCoin = amount + 1
if j - coin >= 0:
dp[j] = min(dp[j - coin] + 1, dp[j])
return dp[amount] if dp[amount] != amount + 1 else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
arr = [float("inf")] * (amount + 1)
arr[0] = 0
for i in range(amount + 1):
for c in coins:
if i + c > amount:
continue
if arr[i + c] > arr[i] + 1:
arr[i + c] = arr[i] + 1
if arr[-1] == float("inf"):
return -1
return arr[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp_table = [float("inf")] * (amount + 1)
return self.dp(dp_table, coins, amount)
def dp(self, dp_table: [float], coins: List[int], amount: int) -> int:
if amount == 0:
return 0
if amount < 0:
return -1
if dp_table[amount] != float("inf"):
return dp_table[amount]
res = float("inf")
for coin in coins:
subpb = self.dp(dp_table, coins, amount - coin)
if subpb == -1:
continue
res = min(res, subpb + 1)
dp_table[amount] = res if res != float("inf") else -1
return dp_table[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF LIST VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
coins.sort()
table = [[(-1) for i in range(amount + 1)] for i in range(len(coins))]
for i in range(len(coins)):
for j in range(amount + 1):
if coins[i] > j and i > 0 and table[i - 1][j] > 0:
table[i][j] = table[i - 1][j]
elif j >= coins[i]:
x = j - coins[i]
pre_val = 9999999
calculated_val = 0
if i > 0 and table[i - 1][j] > 0:
pre_val = table[i - 1][j]
if x > 0:
if table[i][x] > 0:
calculated_val = table[i][x]
table[i][j] = min(1 + calculated_val, pre_val)
elif i > 0 and pre_val != 9999999:
table[i][j] = pre_val
elif x == 0:
table[i][j] = min(1 + calculated_val, pre_val)
return table[len(coins) - 1][amount] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def do_du_tab(self, coins, amount):
dp_tab = [math.inf for _ in range(amount + 1)]
dp_tab[0] = 0
for a in range(1, amount + 1):
potential = []
for coin in coins:
potential.append(dp_tab[a - coin] + 1)
dp_tab[a] = min(potential)
return dp_tab[-1] if dp_tab[-1] != float("inf") else -1
def do_something(self, coins, amount):
dp_tab = [math.inf for _ in range(amount + 1)]
dp_tab[0] = 0
for i in range(1, amount + 1):
temp = []
for coin in coins:
temp.append(dp_tab[i - coin])
dp_tab[i] = min(temp) + 1
return dp_tab[amount] if dp_tab[amount] != float("inf") else -1
def coinChange(self, coins: List[int], amount: int) -> int:
if len(coins) == 0:
return 0
if amount == 0:
return 0
c = [coin for coin in coins if coin <= amount]
if len(c) == 0:
return -1
return self.do_something(c, amount) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [(amount + 1) for i in range(amount + 1)]
dp[0] = 0
for i in range(amount + 1):
for c in coins:
if c <= i and dp[i] > dp[i - c] + 1:
dp[i] = dp[i - c] + 1
return -1 if dp[amount] > amount else dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR NUMBER VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [(-1) for i in range(amount + 1)]
dp[0] = 0
for i in range(amount):
mn = math.inf
pos = False
for c in coins:
if i + 1 >= c and dp[i - c + 1] >= 0:
pos = True
mn = min(mn, dp[i + 1 - c] + 1)
if pos:
dp[i + 1] = mn
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | import sys
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
res = self.helper(sorted(coins, reverse=True), amount, {})
if res == float("inf"):
return -1
return res
def helper(self, coins, amount, dAmounts):
if amount < 0:
return float("inf")
if amount == 0:
return 0
if amount in dAmounts:
return dAmounts[amount]
for c in coins:
pathCount = 1 + self.helper(coins, amount - c, dAmounts)
dAmounts[amount] = min(dAmounts.get(amount, pathCount), pathCount)
return dAmounts[amount] | IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR DICT IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
@lru_cache(None)
def recurse(amount, pos):
if pos == len(coins):
return 10000
if amount == 0:
return 0
if coins[pos] <= amount:
return min(
recurse(amount - coins[pos], pos) + 1, recurse(amount, pos + 1)
)
else:
return recurse(amount, pos + 1)
count = recurse(amount, 0)
if count >= 10000:
return -1
return count | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
myQueue = [[0, 0]]
reachedMap = {0}
for value, num_coins in myQueue:
for coin in coins:
if coin + value in reachedMap:
continue
if coin + value == amount:
return num_coins + 1
if coin + value < amount:
reachedMap.add(value + coin)
myQueue.append([coin + value, num_coins + 1])
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not amount:
return 0
coins = [coin for coin in coins if coin <= amount]
if not coins:
return -1
dp = [(0) for i in range(amount + max(coins) + 10)]
for coin in coins:
dp[coin] = 1
for i in range(1, amount + 1):
for coin in coins:
if dp[i] >= 1:
if dp[i + coin] == 0:
dp[i + coin] = dp[i] + 1
elif dp[i] + 1 < dp[i + coin]:
dp[i + coin] = dp[i] + 1
if not dp[amount]:
return -1
return dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
memo = {}
def dp(m):
if m in coins:
return 1
if m == 0:
return 0
if m < 0:
return float("inf")
if m not in memo:
ans = float("inf")
for v in coins:
ans = min(1 + dp(m - v), ans)
memo[m] = ans
return memo[m]
return dp(amount) if dp(amount) != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
table = [0] + [(amount + 1) for _ in range(amount)]
for i in range(1, amount + 1):
for coin in coins:
if coin <= i:
sub_result = table[i - coin]
if sub_result + 1 < table[i]:
table[i] = sub_result + 1
print(table)
if table[amount] > amount:
return -1
else:
return table[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
Visited = {amount: 0}
coins.sort(reverse=True)
queue = deque()
queue.append(amount)
while queue:
curr = queue.popleft()
for coin in coins:
if curr - coin >= 0 and curr - coin not in Visited:
queue.append(curr - coin)
Visited[curr - coin] = Visited[curr] + 1
if 0 in Visited:
return Visited[0]
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF NUMBER VAR RETURN VAR NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
ary = [amount + 1] * (amount + 1)
ary[0] = 0
for i in range(1, len(ary)):
for coin in coins:
if i - coin < 0:
continue
ary[i] = min(ary[i], ary[i - coin] + 1)
return ary[amount] if ary[amount] < amount + 1 else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
a = [amount + 1] * (amount + 1)
a[0] = 0
for i in range(1, amount + 1):
mlist = [a[i - c] for c in coins if i - c >= 0]
if len(mlist) > 0:
a[i] = 1 + min(mlist)
print(a)
if a[amount] > amount:
return -1
else:
return a[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
def coinchangehelper(rem):
if rem < 0:
return -1
if rem == 0:
return 0
if rem in memo:
return memo[rem]
count = float("inf")
for coin in coins:
sub = coinchangehelper(rem - coin)
if sub >= 0:
count = min(count, 1 + sub)
memo[rem] = count
return memo[rem]
memo = {}
res = coinchangehelper(amount)
return res if res != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not amount:
return 0
coins.sort()
self.coins = coins
self.memo = {}
self.recursion(amount)
return -1 if self.memo[amount] == float("inf") else self.memo[amount]
def recursion(self, amt):
min_coins = float("inf")
if amt == 0:
return 0
if amt in self.memo:
return self.memo[amt]
for coin in self.coins:
if coin > amt:
break
x = self.recursion(amt - coin)
min_coins = min(min_coins, x)
self.memo[amt] = 1 + min_coins
return self.memo[amt] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_CALL VAR STRING NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution(object):
def coinChange(self, coins, amount):
MAX = float("inf")
dp = [0] + [MAX] * amount
for i in range(1, amount + 1):
dp[i] = min([(dp[i - c] if i - c >= 0 else MAX) for c in coins]) + 1
if dp[amount] == MAX:
return -1
return dp[amount]
return [dp[amount], -1][dp[amount] == MAX] | CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR RETURN LIST VAR VAR NUMBER VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
self.coins = coins
self.amount = amount
self.cache = {}
res = self.getValue(self.amount)
if res is None:
return -1
else:
return res
def getValue(self, amount):
if amount in self.cache:
return self.cache[amount]
if amount < 0:
return None
if amount == 0:
return 0
if amount in self.coins:
return 1
min_count = None
for coin in self.coins:
count = self.getValue(amount - coin)
if count is not None:
if min_count is None or min_count > count + 1:
min_count = count + 1
self.cache[amount] = min_count
return min_count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE RETURN NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NONE IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NONE IF VAR NONE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChangeCopy(self, coins: List[int], amount: int) -> int:
if not amount:
return 0
dp = [amount + 1] * (amount + 1)
for i in range(amount + 1):
if i in coins:
dp[i] = 1
continue
candidates = [(dp[i - coin] + 1) for coin in coins if i - coin > 0]
if candidates:
dp[i] = min(candidates)
print(dp)
return -1 if dp[amount] > amount else dp[amount]
def coinChange(self, coins, amount):
if not amount:
return 0
coin_nums = [amount + 1] * (amount + 1)
coins_set = set(coins)
for i in range(amount + 1):
if i in coins_set:
coin_nums[i] = 1
continue
candidates = [(coin_nums[i - coin] + 1) for coin in coins if i - coin > 0]
if candidates:
coin_nums[i] = min(candidates)
return coin_nums[amount] if coin_nums[amount] <= amount else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR VAR NUMBER |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
def dfs(start, amount, n_coins):
coin = coins[start]
div = amount // coin
amount %= coin
n_coins += div
if amount == 0:
self.minimum = min(n_coins, self.minimum)
return
if start < length:
dfs(start + 1, amount, n_coins)
next_coin = coins[start + 1]
for _ in range(div):
amount += coin
n_coins -= 1
if (self.minimum - n_coins - 1) * next_coin + 1 > amount:
dfs(start + 1, amount, n_coins)
else:
break
coins.sort(reverse=True)
self.minimum = float("inf")
length = len(coins) - 1
dfs(0, amount, 0)
return self.minimum if self.minimum < float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
visited = set([amount])
candidates = [amount]
res = 0
while candidates:
res += 1
next_candidates = []
for candidate in candidates:
for coin in coins:
if candidate - coin == 0:
return res
elif candidate - coin > 0 and candidate - coin not in visited:
next_candidates.append(candidate - coin)
visited.add(candidate - coin)
candidates = next_candidates
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], target: int) -> int:
table = [
[float("inf") for _ in range(target + 1)] for _ in range(len(coins) + 1)
]
for i in range(len(coins) + 1):
table[i][0] = 0
for i in range(1, len(coins) + 1):
for j in range(target + 1):
if coins[i - 1] <= j:
a = 1 + table[i][j - coins[i - 1]]
else:
a = float("inf")
b = table[i - 1][j]
if a <= b:
table[i][j] = a
else:
table[i][j] = b
return table[-1][-1] if table[-1][-1] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
def make_change(sum_remaining, coins, mem):
if sum_remaining < 0:
return -1
elif sum_remaining == 0:
return 0
elif mem[sum_remaining]:
return mem[sum_remaining]
else:
min_coins = amount + 1
for coin in coins:
if sum_remaining - coin >= 0:
prev_coins = make_change(sum_remaining - coin, coins, mem)
curr_coins = 1 + prev_coins
if curr_coins > 0 and curr_coins < min_coins:
min_coins = curr_coins
mem[sum_remaining] = min_coins
return mem[sum_remaining]
mem = [None] * (amount + 1)
ans = make_change(amount, coins, mem)
if ans == amount + 1:
return -1
else:
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
values = [(-1) for i in range(amount + 1)]
values[0] = 0
for i in coins:
for j in range(1, len(values)):
if j >= i:
if values[j - i] == -1:
continue
curr_num_coins = 1 + values[j - i]
if values[j] == -1 or values[j] > curr_num_coins:
values[j] = curr_num_coins
return values[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
curr = [float("inf")] * (amount + 1)
curr[0] = 0
coins = sorted(coins)
for num in range(amount + 1):
for c in coins:
if num - c < 0:
break
curr[num] = min(curr[num], curr[num - c] + 1)
return curr[amount] if curr[amount] < float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
m = Counter(coins)
def recurse(left, count) -> int:
if left in m:
return m[left]
if left == 0:
m[left] = count
return count
smallest = math.inf
for c in coins:
if left - c >= 0:
smallest = min(smallest, recurse(left - c, count + 1))
m[left] = smallest + 1
return smallest + 1
recurse(amount, 0)
if m[amount] == math.inf:
return -1
return m[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [[float("INF") for i in range(amount + 1)] for j in range(len(coins) + 1)]
for i in range(len(coins) + 1):
dp[i][0] = 0
for i in range(1, len(coins) + 1):
for j in range(1, amount + 1):
if j - coins[i - 1] < 0:
dp[i][j] = dp[i - 1][j]
else:
dp[i][j] = min(dp[i - 1][j], dp[i][j - coins[i - 1]] + 1)
if dp[len(coins)][amount] == float("INF"):
return -1
else:
return dp[len(coins)][amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR FUNC_CALL VAR VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort(reverse=True)
self.min = 2**31 - 1
def dfs(index, amount, count):
if amount == 0:
self.min = min(self.min, count)
if amount < 0:
return
for i in range(index, len(coins)):
if coins[i] * (self.min - count) > amount >= coins[i]:
dfs(i, amount - coins[i], count + 1)
dfs(0, amount, 0)
return self.min if self.min != 2**31 - 1 else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: [int], amount: int) -> int:
self.ans = float("inf")
self.dict = {}
coins.sort(reverse=True)
def helper(num, depth):
if num == 0:
self.ans = min(self.ans, depth)
return
for c in coins:
res = num - c
if res >= 0:
if res in self.dict:
if self.dict[res] > depth + 1 and depth + 1 < self.ans:
self.dict[res] = depth + 1
helper(res, depth + 1)
else:
self.dict[res] = depth + 1
helper(res, depth + 1)
helper(amount, 0)
return self.ans if self.ans < float("inf") else -1 | CLASS_DEF FUNC_DEF LIST VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def __init__(self):
self.memo = {}
def coinChange(self, coins: List[int], amount: int) -> int:
ans = self.helper(coins, amount)
if ans == float("inf"):
return -1
return ans
def helper(self, coins, remaining):
if remaining == 0:
return 0
if remaining < 0:
return float("inf")
if remaining in self.memo:
return self.memo[remaining]
self.memo[remaining] = (
min([self.helper(coins, remaining - i) for i in coins[::-1]]) + 1
)
return self.memo[remaining] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not coins or amount < 0:
return -1
if amount == 0:
return 0
q = [(amount, 0)]
visited = {0}
coins.sort(reverse=True)
while q:
node, depth = q.pop(0)
for coin in coins:
rest = node - coin
if rest == 0:
return depth + 1
if rest > 0 and rest not in visited:
q.append((rest, depth + 1))
visited.add(rest)
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange_bottomUp(self, coins: List[int], amount: int) -> int:
lookup = {x: (amount + 1) for x in range(1, amount + 1)}
lookup[0] = 0
for i in range(amount + 1):
for coin in coins:
remainder = i - coin
if remainder < 0:
continue
best_min = min(lookup[remainder] + 1, lookup[i])
lookup[i] = best_min
if lookup[i] > amount:
return -1
else:
return lookup[i]
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort()
coins.reverse()
lookup = {}
def find_combos(total_remain, total_coins):
if total_remain in lookup:
if lookup[total_remain] > total_coins:
lookup[total_remain] = total_coins
else:
return
else:
lookup[total_remain] = total_coins
for coin in coins:
if total_remain - coin < 0:
continue
find_combos(total_remain - coin, total_coins + 1)
find_combos(amount, 0)
return lookup[0] if 0 in lookup else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN ASSIGN VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def do_du_tab(self, coins, amount):
dp_tab = [math.inf for _ in range(amount + 1)]
dp_tab[0] = 0
for i in range(len(coins)):
for a in range(1, amount + 1):
if a >= coins[i]:
dp_tab[a] = min(dp_tab[a], 1 + dp_tab[a - coins[i]])
return dp_tab[amount]
def do_td_mem(self, cache, coins, amount, index):
if amount == 0:
return 0
if len(coins) <= index:
return math.inf
if cache[index][amount] == math.inf:
count_keeping_element = math.inf
if coins[index] <= amount:
temp = self.do_td_mem(cache, coins, amount - coins[index], index)
if temp != math.inf:
count_keeping_element = temp + 1
count_skipping_element = self.do_td_mem(cache, coins, amount, index + 1)
cache[index][amount] = min(count_keeping_element, count_skipping_element)
return cache[index][amount]
def do_bf(self, coins, amount, index):
if amount == 0:
return 0
n = len(coins)
if n <= index:
return math.inf
count_keeping_element = math.inf
if coins[index] <= amount:
temp = self.do_bf(coins, amount - coins[index], index)
if temp != math.inf:
count_keeping_element = temp + 1
count_skipping_element = self.do_bf(coins, amount, index + 1)
return min(count_keeping_element, count_skipping_element)
def coinChange(self, coins: List[int], amount: int) -> int:
if amount < 1:
return 0
coins.sort()
output = self.do_du_tab(coins, amount)
return -1 if output == math.inf else output | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def helper(self, coins, amount, total, count, mem):
if total > amount:
return
if total == amount:
if total not in mem:
mem[total] = count
else:
mem[total] = min(mem[total], count)
return
for c in coins:
if total + c not in mem or mem[total + c] > count + 1:
mem[total + c] = count + 1
self.helper(coins, amount, total + c, count + 1, mem)
return
def coinChange(self, coins: List[int], amount: int) -> int:
if not amount:
return 0
if amount < min(coins):
return -1
mem = {}
coins.sort(reverse=True)
self.helper(coins, amount, 0, 0, mem)
if amount in mem:
return mem[amount]
else:
return -1 | CLASS_DEF FUNC_DEF IF VAR VAR RETURN IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FOR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR IF VAR VAR RETURN VAR VAR RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def recursion(self, coins, remain, dic):
if remain < 0:
return float("inf")
if remain == 0:
return 0
if remain in dic.keys():
return dic[remain]
min_coin = float("inf")
for coin in coins:
number_coin = None
prev_num = self.recursion(coins, remain - coin, dic)
if prev_num == float("inf"):
number_coin = prev_num
else:
number_coin = prev_num + 1
min_coin = min(min_coin, number_coin)
dic[remain] = min_coin
return min_coin
def coinChange(self, coins: List[int], amount: int) -> int:
remain = amount
count = 0
dic = {}
number = self.recursion(coins, remain, dic)
if number == float("inf"):
return -1
else:
return number | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
def coinChangeHelper(coins, amount, memoisation={}):
if amount == 0:
return 0
if amount < 0:
return float("inf")
if amount in memoisation:
return memoisation[amount]
min_coins_used = float("inf")
for i in range(len(coins) - 1, -1, -1):
min_coins_used = min(
1 + coinChangeHelper(coins, amount - coins[i], memoisation),
min_coins_used,
)
memoisation[amount] = min_coins_used
return min_coins_used
result = coinChangeHelper(coins, amount)
return result if result != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF DICT IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
self.dp = [([-1] * (amount + 1)) for _ in range(len(coins) + 1)]
value = self.solve(coins, len(coins), amount)
return self.dp[-1][-1] if value != float("inf") else -1
def solve(self, coins, n, amount):
if amount == 0:
self.dp[n][amount] = 0
return 0
if n == 0:
self.dp[n][amount] = float("inf")
return self.dp[n][amount]
if self.dp[n][amount] != -1:
return self.dp[n][amount]
if coins[n - 1] <= amount:
self.dp[n][amount] = min(
1 + self.solve(coins, n, amount - coins[n - 1]),
self.solve(coins, n - 1, amount),
)
return self.dp[n][amount]
else:
self.dp[n][amount] = self.solve(coins, n - 1, amount)
return self.dp[n][amount] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER NUMBER NUMBER VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
coins.sort(reverse=True)
queue = [amount]
visit = {amount: 0}
while queue:
remain = queue.pop(0)
for c in coins:
branch = remain - c
if 0 <= branch not in visit:
queue.append(branch)
visit.update({branch: visit[remain] + 1})
return visit.get(0) or -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR DICT VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR DICT VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def helper(self, amount: int) -> int:
if amount in list(self.cache.keys()):
return self.cache[amount]
counts = []
for coin in self.coins:
if amount - coin > 0:
counts.append(self.helper(amount - coin) + 1)
elif amount - coin == 0:
counts.append(1)
break
if counts == []:
self.cache[amount] = sys.maxsize
else:
self.cache[amount] = min(counts)
return self.cache[amount]
def coinChange(self, coins: List[int], amount: int) -> int:
self.coins = coins
if amount == 0:
return 0
self.cache = {}
res = self.helper(amount)
return res if res < 100000000 else -1 | CLASS_DEF FUNC_DEF VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def util(self, memo, coins, amount):
if amount == 0:
return 0
if amount < 0:
return -1
minarr = []
for coin in coins:
if amount - coin not in memo:
memo[amount - coin] = self.util(memo, coins, amount - coin)
minarr.append(memo[amount - coin])
minarr = [m for m in minarr if m != -1]
if not minarr:
return -1
return min(minarr) + 1
def coinChange(self, coins: List[int], amount: int) -> int:
memo = {}
return self.util(memo, coins, amount) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
numCoins = [float("inf") for i in range(amount)]
for coin in coins:
if coin - 1 < amount:
numCoins[coin - 1] = 1
for i in range(amount):
minCoin = float("inf")
change = True
for coin in coins:
if i == coin - 1:
change = False
break
if i - coin >= 0:
minCoin = min(numCoins[i - coin], minCoin)
if change and minCoin != float("inf"):
numCoins[i] = minCoin + 1
if numCoins[-1] == float("inf"):
return -1
return numCoins[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coinsNeeded = {}
coinsNeeded[0] = 0
for n in coins:
coinsNeeded[n] = 1
def findMinCoins(amount):
if amount < 0:
return float("inf")
if amount in coinsNeeded:
return coinsNeeded[amount]
for n in coins:
coinsUsed = 1 + findMinCoins(amount - n)
if amount in coinsNeeded:
coinsNeeded[amount] = min(coinsUsed, coinsNeeded[amount])
else:
coinsNeeded[amount] = coinsUsed
return coinsNeeded[amount]
findMinCoins(amount)
if coinsNeeded[amount] == float("inf"):
return -1
return coinsNeeded[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR RETURN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount < 1:
return 0
self.coins = sorted(coins, reverse=True)
self.count = {}
return self.helper(amount)
def helper(self, rem):
if rem < 0:
return -1
if rem == 0:
return 0
if rem in self.count:
return self.count[rem]
min_count = 1000000000
for coin in self.coins:
res = self.helper(rem - coin)
if res >= 0 and res < min_count:
min_count = res + 1
if min_count == 1000000000:
self.count[rem] = -1
else:
self.count[rem] = min_count
return self.count[rem] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
d = {}
ans = self.helper(coins, amount, d)
if ans == 100000000:
return -1
return ans
def helper(self, coins, amount, d):
if amount == 0:
return 0
ans = 100000000
for i in range(len(coins) - 1, -1, -1):
if coins[i] <= amount:
if amount - coins[i] in d:
sub_ans = d[amount - coins[i]]
else:
sub_ans = 1 + self.helper(coins, amount - coins[i], d)
d[amount - coins[i]] = sub_ans
ans = min(ans, sub_ans)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
memo = [[float("inf") for _ in range(amount + 1)] for _ in range(len(coins))]
for x in range(len(coins)):
memo[x][0] = 0
for i, coin in enumerate(coins):
for tot in range(1, amount + 1):
if i > 0:
memo[i][tot] = memo[i - 1][tot]
if coin <= tot:
memo[i][tot] = min(memo[i][tot], memo[i][tot - coin] + 1)
out = memo[len(coins) - 1][amount]
return -1 if out == float("inf") else out | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not coins:
return -1
n = len(coins)
if amount == 0:
return 0
fewest = [([0] * n) for k in range(amount + 1)]
for k in range(n):
fewest[0][k] = 0
for p in range(1, amount + 1):
is_divisible = p % coins[0] == 0
if is_divisible:
fewest[p][0] = p // coins[0]
else:
fewest[p][0] = -1
for k in range(1, n):
for p in range(1, amount + 1):
if p < coins[k]:
fewest[p][k] = fewest[p][k - 1]
elif fewest[p - coins[k]][k] == -1:
fewest[p][k] = fewest[p][k - 1]
elif fewest[p][k - 1] == -1:
fewest[p][k] = fewest[p - coins[k]][k] + 1
else:
fewest[p][k] = min(fewest[p - coins[k]][k] + 1, fewest[p][k - 1])
return fewest[p][n - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
memo = {}
def helper(target, options):
if target in memo:
return memo[target]
else:
best = None
for opt in options:
if opt == target:
memo[target] = 1
return 1
elif opt < target:
cont = helper(target - opt, options)
if cont is not None and (best is None or cont + 1 < best):
best = cont + 1
memo[target] = best
return best
output = helper(amount, coins)
return output if output is not None else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NONE FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NONE VAR NONE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NONE VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
round = 0
d = set([0])
while d or round == 0:
round += 1
d = set(
[
(coin + s)
for coin in coins
for s in d
if coin + s <= amount and coin + s not in d
]
)
if amount in d:
return round
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins = set(coins)
d = {}
if amount == 0:
return 0
def solve(amt):
if amt in d:
return d[amt]
if amt <= 0:
return -1
if amt in coins:
d[amt] = 1
return d[amt]
poss = []
for c in coins:
search = amt - c
if search < 1:
continue
val = solve(search)
if val != -1:
poss.append(val)
if len(poss) != 0:
d[amt] = 1 + min(poss)
else:
d[amt] = -1
return d[amt]
return solve(amount) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT IF VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0] + [float("inf")] * amount
for c in coins:
for j in range(c, amount + 1):
dp[j] = min(dp[j], dp[j - c] + 1)
return dp[-1] if dp[-1] < float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coinsCount = dict()
for i in range(1, amount + 1):
coinsCount[i] = 9999
coinsCount[0] = 0
for i in range(1, amount + 1):
for icoin in range(0, len(coins)):
curDenom = coins[icoin]
difference = i - curDenom
if difference >= 0:
coinsCount[i] = min(1 + coinsCount[difference], coinsCount[i])
elif difference == 0:
coinsCount[i] += 1
if coinsCount[amount] != 9999:
return coinsCount[amount]
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins = sorted(coins, reverse=True)
dp = [-1] * (amount + 1)
dp[0] = 0
for i in range(1, amount + 1):
if i - coins[-1] < 0:
continue
for coin in coins:
index = i - coin
if index >= 0 and dp[index] != -1:
value = dp[index] + 1
if dp[i] == -1 or dp[i] > value:
dp[i] = value
return dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
tracker = [float("inf") for i in range(amount)]
res = self.coinChange_helper(coins, amount, tracker)
print(tracker)
if tracker[-1] == float("inf"):
return -1
return res
def coinChange_helper(
self, coins: List[int], amount: int, tracking: List[int]
) -> int:
if amount < 0:
return -1
if amount == 0:
return 0
if tracking[amount - 1] != float("inf"):
return tracking[amount - 1]
lowest = float("inf")
for i in range(len(coins)):
result = self.coinChange_helper(coins, amount - coins[i], tracking)
if result >= 0 and result + 1 < lowest:
lowest = result + 1
tracking[amount - 1] = lowest if lowest != float("inf") else -1
return tracking[amount - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR STRING VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort(reverse=True)
return self.dfs(coins, 0, 0, amount, -1)
def dfs(
self, coins: List[int], idx: int, cnt: int, amount: int, minCnt: int
) -> int:
if amount == 0:
return cnt
if idx >= len(coins):
return -1
if minCnt > 0 and cnt + amount // coins[idx] + 1 > minCnt:
return -1
for i in range(amount // coins[idx], -1, -1):
res = self.dfs(coins, idx + 1, cnt + i, amount - coins[idx] * i, minCnt)
if res != -1:
minCnt = res if minCnt == -1 else min(minCnt, res)
return minCnt | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not coins and amount:
return -1
if not amount:
return 0
target = [0] * (amount + 1)
target[0] = 1
for i in range(len(coins) - 1, -1, -1):
coin = coins[i]
j = 0
while j <= amount:
if target[j] == 0:
j += 1
continue
next_idx = j + coin
if next_idx > amount:
break
if (
target[j] > 0
and target[next_idx] > target[j]
or target[next_idx] == 0
):
target[next_idx] = target[j] + 1
j += 1
print(target)
if target[amount]:
return target[amount] - 1
else:
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, denoms: List[int], n: int) -> int:
if n == 0:
return 0
nodes = set([0])
seen = set()
count = 1
while nodes:
seen = seen | nodes
next_nodes = set()
for node in nodes:
for denom in denoms:
candidate = node + denom
if candidate == n:
return count
elif candidate < n and candidate not in seen:
next_nodes.add(candidate)
count += 1
nodes = next_nodes
return -1
pass | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
dp = [None] * amount
dp[0] = 1 if 1 in coins else -1
for i in range(1, amount):
min_req = float("inf")
for c in coins:
if (i + 1) % c == 0:
min_req = min(min_req, (i + 1) // c)
elif (i + 1) // c > 0 and dp[i - c] != -1:
min_req = min(min_req, dp[i - c] + 1)
if min_req == float("inf"):
dp[i] = -1
else:
dp[i] = min_req
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | import sys
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [sys.maxsize] * (amount + 1)
if amount == 0:
return 0
dp[amount] = 0
for i in range(amount, -1, -1):
for coin in coins:
if i - coin >= 0:
dp[i - coin] = min(dp[i] + 1, dp[i - coin])
return -1 if dp[0] == sys.maxsize else dp[0] | IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR NUMBER VAR NUMBER VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
dp = [-1] * (amount + 1)
dp[0] = 0
for i in range(len(coins)):
if coins[i] <= amount:
dp[coins[i]] = 1
for i in range(1, amount + 1):
if dp[i] == -1:
comparison = []
for c in coins:
if i - c >= 0 and dp[i - c] != -1:
comparison.append(dp[i - c])
if comparison:
dp[i] = min(comparison) + 1
return dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount < 1:
return 0
cnt = [(0) for __ in range(amount)]
return self.coinMake(coins, amount, cnt)
def coinMake(self, coins, rem, count):
if rem < 0:
return -1
if rem == 0:
return 0
if count[rem - 1] != 0:
return count[rem - 1]
MIN = 2**32 - 1
for i in coins:
res = self.coinMake(coins, rem - i, count)
if res >= 0 and res < MIN:
MIN = 1 + res
count[rem - 1] = -1 if MIN == 2**32 - 1 else MIN
return count[rem - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER VAR RETURN VAR BIN_OP VAR NUMBER |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not amount:
return 0
queue = deque([(0, 0)])
visited = [True] + [False] * amount
while queue:
totalCoins, currVal = queue.popleft()
totalCoins += 1
for coin in coins:
nextVal = currVal + coin
if nextVal == amount:
return totalCoins
if nextVal < amount:
if not visited[nextVal]:
visited[nextVal] = True
queue.append((totalCoins, nextVal))
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
num_ways = (amount + 1) * [-1]
num_ways[0] = 0
for v in range(1, amount + 1):
fewest = float("inf")
for c in coins:
if v - c >= 0 and num_ways[v - c] != -1:
fewest = min(num_ways[v - c], fewest)
if fewest != float("inf"):
num_ways[v] = 1 + fewest
return num_ways[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP NUMBER VAR RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def aux(self, coins, amount, cache):
if amount in cache:
return cache[amount]
res = -1
for i in range(len(coins)):
if coins[i] < amount:
right = (
cache[amount - coins[i]]
if amount - coins[i] in cache
else self.aux(coins, amount - coins[i], cache)
)
if right != -1 and res == -1:
res = 1 + right
elif right != -1 and res != -1:
res = min(res, 1 + right)
cache[amount] = res
return cache[amount]
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
cache = dict(list(zip(coins, [1] * len(coins))))
return self.aux(coins, amount, cache) | CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
value1 = [0]
value2 = []
nc = 0
visited = [False] * (amount + 1)
visited[0] = True
while value1:
nc += 1
for v in value1:
for coin in coins:
newval = v + coin
if newval <= amount:
if not visited[newval]:
if newval == amount:
return nc
visited[newval] = True
value2.append(newval)
value1, value2 = value2, []
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [-1] * (amount + 1)
dp[0] = 0
for idx in range(amount + 1):
tmp = []
for coin in coins:
if idx - coin >= 0 and dp[idx - coin] != -1:
tmp.append(dp[idx - coin])
if tmp:
dp[idx] = min(tmp) + 1
print(dp)
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
D = [0] * (amount + 1)
def helper(rem):
nonlocal D
if rem < 0:
return -1
if rem == 0:
return 0
if D[rem] != 0:
return D[rem]
minn = float("inf")
for coin in coins:
res = helper(rem - coin)
if res >= 0 and res < minn:
minn = res + 1
if minn == float("inf"):
D[rem] = -1
else:
D[rem] = minn
return D[rem]
return helper(amount) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dic = {(0): 0}
def change(amount):
if amount < 0:
return -1
if amount in dic:
return dic[amount]
res = [change(amount - i) for i in coins if change(amount - i) >= 0]
if not res:
dic[amount] = -1
else:
dic[amount] = min(res) + 1
return dic[amount]
return change(amount) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
count = collections.defaultdict(int)
def coinChangeHelper(coins, rem, count):
if rem < 0:
return -1
if rem == 0:
return 0
if count[rem] != 0:
return count[rem]
minCount = 2**32
for coin in coins:
makeChange = coinChangeHelper(coins, rem - coin, count)
if makeChange > -1 and makeChange < minCount:
minCount = makeChange + 1
count[rem] = -1 if minCount == 2**32 else minCount
return count[rem]
return coinChangeHelper(coins, amount, count) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER NUMBER VAR RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not amount:
return 0
queue = collections.deque([(0, 0)])
seen = set()
while queue:
curAmount, step = queue.popleft()
if curAmount == amount:
return step
for j in range(len(coins)):
newAmount = curAmount + coins[j]
if newAmount <= amount and newAmount not in seen:
seen.add(newAmount)
queue.append((newAmount, step + 1))
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
coins.sort()
coins.reverse()
print(coins)
change = [-1] * (amount + 1)
change[0] = 0
for i in range(1, amount + 1):
for c1 in coins:
if c1 > i:
continue
c2 = change[i - c1]
if c2 >= 0:
if change[i] == -1:
change[i] = c2 + 1
else:
change[i] = min(c2 + 1, change[i])
return change[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
memo = {}
def _fewest(cs, amt):
nonlocal memo
if amt not in memo:
if amt < 0:
memo[amt] = -1
elif amt == 0:
memo[amt] = 0
else:
fewest = -1
for c in cs:
f = _fewest(cs, amt - c)
if f != -1 and (fewest == -1 or f + 1 < fewest):
fewest = f + 1
memo[amt] = fewest
return memo[amt]
return _fewest(coins, amount) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort()
n_coins = len(coins)
least_coin = coins[0]
max_amount = amount + 1
mat = [[(0) for i in range(amount + 1)] for i in range(n_coins + 1)]
if amount == 0:
return 0
if amount < least_coin:
return -1
for i in range(amount + 1):
if i % least_coin == 0:
mat[1][i] = i // least_coin
else:
mat[1][i] = -1
for i in range(2, n_coins + 1):
for j in range(1, amount + 1):
curr_coin = coins[i - 1]
if j - curr_coin >= 0:
if mat[i][j - curr_coin] == -1:
mat[i][j] = mat[i - 1][j]
elif mat[i - 1][j] == -1:
mat[i][j] = 1 + mat[i][j - curr_coin]
elif mat[i][j - curr_coin] == -1 and mat[i - 1][j] == -1:
mat[i][j] = -1
else:
mat[i][j] = min(1 + mat[i][j - curr_coin], mat[i - 1][j])
else:
mat[i][j] = mat[i - 1][j]
return mat[-1][-1] | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort(reverse=True)
d = {}
for v in coins:
d[v] = True
min_coin = coins[-1]
if not coins or not amount:
return 0
if len(coins) == 1:
if amount % coins[0] == 0:
return int(amount / coins[0])
else:
return -1
self.memo = {}
return self.fewest(coins, amount, d, min_coin)
def fewest(self, coins, amount, d_value, min_coin):
if amount in self.memo:
return self.memo[amount]
if amount in d_value:
return 1
elif amount < min_coin:
return -1
_min = float("inf")
for v in coins:
ret = self.fewest(coins, amount - v, d_value, min_coin)
if ret > 0:
_min = min(_min, ret + 1)
_min = -1 if _min == float("inf") else _min
self.memo[amount] = _min
return _min | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
memo = [[(-1) for _ in range(amount + 1)] for _ in coins]
for i in range(len(coins) - 1, -1, -1):
for j in range(amount + 1):
if j == 0:
memo[i][j] = 0
continue
if (
j - coins[i] >= 0
and memo[i][j - coins[i]] != -1
and i + 1 < len(coins)
and memo[i + 1][j] != -1
):
memo[i][j] = min(memo[i][j - coins[i]] + 1, memo[i + 1][j])
elif j - coins[i] >= 0 and memo[i][j - coins[i]] != -1:
memo[i][j] = memo[i][j - coins[i]] + 1
elif i + 1 < len(coins) and memo[i + 1][j] != -1:
memo[i][j] = memo[i + 1][j]
return memo[0][amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
memo = {}
def backtrack(memo, curr=amount):
if curr == 0:
return 0
if memo.get(curr):
return memo[curr]
minimum = math.inf
for coin in coins:
if curr - coin < 0:
continue
res = backtrack(memo, curr - coin)
minimum = min(res, minimum)
minimum = minimum if minimum == math.inf else minimum + 1
memo[curr] = minimum
return minimum
ans = backtrack(memo)
if ans == math.inf:
return -1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.