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: dp = {} def recursion(S): if S == 0: return 0 if S < 0: return float("inf") if S in dp: return dp[S] x = float("inf") for coin in coins: x = min(x, 1 + recursion(S - coin)) dp[S] = x return x res = recursion(amount) if res == float("inf"): return -1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF 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 VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL 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: if not amount: return 0 q = deque([amount]) hs = [False] * amount count = 0 while q: l = len(q) while l: n = q.popleft() for c in coins: x = n - c if not x: return count + 1 if x > 0 and not hs[x]: hs[x] = True q.append(x) l -= 1 count += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR RETURN BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER 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 self.dp = {} min_ = sys.maxsize for coin in coins: if amount - coin >= 0: min_ = min(min_, 1 + self.change(amount - coin, coins)) if min_ == sys.maxsize: return -1 return min_ def change(self, amount, coins): if amount in self.dp: return self.dp[amount] if amount == 0: self.dp[amount] = 0 return 0 if amount in coins: self.dp[amount] = 1 return 1 min_ = sys.maxsize for coin in coins: if amount - coin >= 0: min_ = min(min_, 1 + self.change(amount - coin, coins)) self.dp[amount] = min_ return min_
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR 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: self.amount = amount self.coins = coins n = len(coins) self.memo = [-1] * (self.amount + 1) val = self.coindp(amount) if val == float("inf"): return -1 else: return val def coindp(self, i): if i < 0: return float("inf") if i == 0: return 0 if self.memo[i] >= 0: return self.memo[i] mini = float("inf") for coin in self.coins: mini = min(mini, self.coindp(i - coin) + 1) self.memo[i] = mini return self.memo[i]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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 NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP 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: memo = dict() def dp(n): if n in memo: return memo[n] if n == 0: return 0 if n < 0: return -1 res = 1000 for coin in coins: if dp(n - coin) == -1: continue res = min(res, 1 + dp(n - coin)) memo[n] = res if res != 1000 else -1 return memo[n] return dp(amount)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER 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: def bfs(): q = [(amount, 0)] if amount == 0: return 0 seen = set() coins.sort(reverse=True) print(coins) while len(q) > 0: a, ci = q.pop(0) if a in seen: continue else: seen.add(a) for c in coins: if a - c == 0: return ci + 1 if a - c > 0: q.append((a - c, ci + 1)) return -1 def dfs(): if amount == 0: return 0 coins.sort() s = [(amount, 0)] while len(s) > 0: a, ci = s.pop() for c in coins: if a - c == 0: return ci + 1 if a - c > 0: s.append((a - c, ci + 1)) return -1 def paste() -> int: queue = deque([]) seen = set() queue.append((0, 0)) while queue: summ, num_coins = queue.popleft() if summ == amount: return num_coins for coin in coins: if summ + coin <= amount and summ + coin not in seen: queue.append((summ + coin, num_coins + 1)) seen.add(summ + coin) return -1 return paste()
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR RETURN VAR FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER VAR RETURN FUNC_CALL 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 __init__(self): self.table = {} def coinChange(self, coins: List[int], amount: int) -> int: if amount < 0: return -1 if amount == 0: return 0 if amount in self.table: return self.table[amount] possible_answers = [ (self.coinChange(coins, amount - coin) + 1) for coin in coins ] possible_answers = [ans for ans in possible_answers if ans >= 1] if possible_answers: ans = min(possible_answers) else: ans = -1 self.table[amount] = ans return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN 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: res = 0 queue = collections.deque([(amount, 0)]) seen = {amount} while queue: head, res = queue.popleft() if head == 0: return res for c in coins: new_amount = head - c if new_amount > -1 and new_amount not in seen: queue.append((new_amount, res + 1)) seen.add(new_amount) return -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER RETURN VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR 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(self, coins: List[int], amount: int) -> int: d = {(0): 0} def dfs(left, num): if left < 0: return -1 if left in d: return d[left] min_ans = math.inf for each in coins: val = dfs(left - each, num + 1) if val >= 0: min_ans = min(min_ans, val) if min_ans == math.inf: d[left] = -1 else: d[left] = min_ans + 1 return d[left] return dfs(amount, 0)
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 VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN 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: if amount == 0: return 0 s = list() s.append(0) for i in range(1, amount + 1): m = list() for j in range(0, len(coins)): if i - coins[j] >= 0: m.append(s[i - coins[j]]) mm = [item for item in m if item >= 0] if len(mm) == 0: s.append(-1) else: s.append(min(mm) + 1) return s[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL 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: res = {} res[0] = 0 for x in range(amount + 1)[1:]: prevs = [(x - c) for c in coins] this_res = min([res.get(y, 2**31) for y in prevs]) + 1 res[x] = this_res if res[amount] >= 2**31: return -1 else: return res[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR BIN_OP NUMBER 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: n = len(coins) if amount == 0: return 0 if amount < min(coins): return -1 l = min(coins) A = [(0) for i in range(amount + 1)] A[l] = 1 for i in range(1, amount + 1): A[i] = float("inf") for coin in coins: if i > coin and A[i - coin] > 0: A[i] = min(A[i], 1 + A[i - coin]) elif i == coin: A[i] = 1 if A[i] == float("inf"): A[i] = 0 print(A) if A[amount] == 0: return -1 return A[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR 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: def my_coinChange(coins, rem, count): if rem < 0: return -1 if rem == 0: return 0 if count[rem - 1] != 0: return count[rem - 1] my_min = float("inf") for coin in coins: res = my_coinChange(coins, rem - coin, count) if res >= 0 and res < my_min: my_min = 1 + res count[rem - 1] = -1 if my_min == float("inf") else my_min return count[rem - 1] if amount < 1: return 0 return my_coinChange(coins, amount, [0] * amount)
CLASS_DEF FUNC_DEF 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 FUNC_CALL VAR STRING 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 FUNC_CALL VAR STRING NUMBER VAR RETURN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP LIST 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: return self.coinChangeBFS(coins, amount) def coinChangeBFS(self, coins: List[int], amount: int) -> int: if amount == 0 or not coins: return 0 queue = deque([amount]) visited = set([amount]) depth = 0 while queue: length = len(queue) depth += 1 for i in range(length): remaining = queue.popleft() for c in coins: if remaining - c == 0: return depth elif remaining - c < 0: continue elif remaining - c not in visited: queue.append(remaining - c) visited.add(remaining - c) return -1
CLASS_DEF FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR VAR VAR IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR LIST 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 IF BIN_OP VAR VAR NUMBER RETURN VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP 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) dp = {} def in_dict(val): return val in dp.keys() and dp[val] if not amount: return 0 if coins[0] > amount: return -1 for coin in coins: dp[coin] = 1 for i in range(coins[0], min(amount, coins[-1]) + 1): if i in dp.keys(): continue available_coins = [coin for coin in coins if coin <= i] possible_min_coins = [ (1 + dp[i - coin]) for coin in available_coins if in_dict(i - coin) ] dp[i] = min(possible_min_coins) if possible_min_coins else 0 for i in range(coins[-1] + 1, amount + 1): possible_min_coins = [ (1 + dp[i - coin]) for coin in coins if in_dict(i - coin) ] dp[i] = min(possible_min_coins) if possible_min_coins else 0 return dp[amount] or -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF RETURN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER IF VAR NUMBER VAR RETURN NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN 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 helper(self, coins, amount, cache, current=[]): if amount == 0: return 0 if amount < 0: return -1 else: if amount in cache: return cache[amount] currMin = sys.maxsize for coin in coins: current.append(coin) currNum = self.helper(coins, amount - coin, cache, current) if currNum != -1: if currNum < currMin: currMin = currNum + 1 current.pop() cache[amount] = currMin return currMin def coinChange(self, coins, amount): cache = {} val = self.helper(coins, amount, cache) memo = [sys.maxsize] * (amount + 1) memo[0] = 0 for i in range(1, amount + 1): for coin in coins: newIndex = i - coin if newIndex >= 0: if memo[newIndex] + 1 < memo[i]: memo[i] = memo[newIndex] + 1 if memo[amount] == sys.maxsize: return -1 else: return memo[amount]
CLASS_DEF FUNC_DEF LIST IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR 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: dp = [float("Inf") for _ in range(amount + 1)] dp[0] = 0 for i in range(1, amount + 1): for coin in coins: if i >= coin and dp[i - coin] != float("Inf"): dp[i] = min(dp[i], dp[i - coin] + 1) if dp[amount] == float("Inf"): return -1 return dp[amount]
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 BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER 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: def change_memo(coins, amount, memo): if amount not in memo: if amount == 0: best = 0 elif amount < 0: best = -1 else: best = None for c in coins: res = change_memo(coins, amount - c, memo) if res == -1: continue if best is None or res < best: best = res if best is None: best = -1 else: best += 1 memo[amount] = best return memo[amount] memo = {} change_memo(coins, amount, memo) return memo[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR DICT 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: @lru_cache(maxsize=None) def _recursive(a): if a == 0: return 0 min_n = 1000000000.0 for c in coins: if c <= a: sub_a = _recursive(a - c) if sub_a != -1: min_n = min(min_n, sub_a + 1) return min_n if min_n != 1000000000.0 else -1 return _recursive(amount)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER VAR NUMBER FUNC_CALL VAR NONE 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] if amount == 0: return 0 ncarr = [] carr = [] for c in coins: if amount >= c: nnc = self.numCoins(coins, amount - c) ncarr.append(nnc + 1) carr.append(c) if len(ncarr) > 0: nc = min(ncarr) self.memory[amount] = nc return nc else: return float("inf") def coinChange(self, coins: List[int], amount: int) -> int: self.memory = {} 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 IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR STRING FUNC_DEF VAR VAR VAR ASSIGN VAR DICT 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: if amount < 0: return -1 coins = sorted(coins) d = [amount + 1] * (amount + 1) d[0] = 0 for i in range(amount + 1): for j in coins: if j <= i: d[i] = min(d[i], d[i - j] + 1) else: break if d[-1] > amount: return -1 else: return d[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR 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 VAR 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], a: int) -> int: if a == 0: return 0 dp = [(-1) for i in range(a + 1)] dp[0] = 1 coins.sort() for i in range(1, len(coins) + 1): for j in range(1, a + 1): if coins[i - 1] == j: dp[j] = 1 else: c = -1 if j - coins[i - 1] > 0 and dp[j - coins[i - 1]] != -1: c = dp[j - coins[i - 1]] + 1 d = dp[j] if c != -1 and d != -1: dp[j] = min(c, d) elif c == -1 and d != -1: dp[j] = d elif c != -1: dp[j] = c if dp[a] == -1: return -1 else: return dp[a]
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR 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: cache = {} def subproblem(i, t): if t == 0: return 0 if (i, t) in cache: return cache[i, t] val = coins[i] if val > t: choice_take = math.inf elif val == t: choice_take = 1 else: choice_take = 1 + subproblem(i, t - val) if i == 0: choice_leave = math.inf else: choice_leave = subproblem(i - 1, t) optimal = min(choice_take, choice_leave) cache[i, t] = optimal return optimal mincoins = subproblem(len(coins) - 1, amount) if mincoins == math.inf: return -1 else: return mincoins
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER 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, amount): coins.sort(reverse=True) lenc, self.res = len(coins), 2**31 - 1 def dfs(pt, rem, count): if not rem: self.res = min(self.res, count) for i in range(pt, lenc): if coins[i] <= rem < coins[i] * (self.res - count): dfs(i, rem - coins[i], count + 1) for i in range(lenc): dfs(i, amount, 0) return self.res if self.res < 2**31 - 1 else -1
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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: n = len(coins) dp = [[float("inf") for _ in range(amount + 1)] for _ in range(n)] for r in range(n): dp[r][0] = 0 for a in range(1, amount + 1): div, mod = divmod(a, coins[0]) if mod == 0: dp[0][a] = div for i in range(1, n): for a in range(1, amount + 1): if a - coins[i] >= 0: dp[i][a] = min(dp[i][a - coins[i]] + 1, dp[i - 1][a]) else: dp[i][a] = dp[i - 1][a] return dp[-1][-1] if dp[-1][-1] != float("inf") else -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR 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 ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER 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, nums: List[int], amount: int) -> int: 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 nums]) + 1 return [dp[amount], -1][dp[amount] == MAX]
CLASS_DEF FUNC_DEF VAR VAR VAR 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 RETURN LIST VAR VAR NUMBER 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 = {} return self.aux(coins, amount, memo) def aux(self, coins, amount, memo): if amount < 0: return -1 if amount == 0: return 0 if amount in memo.keys(): return memo[amount] minimum = 999999999 for x in coins: temp = self.aux(coins, amount - x, memo) if temp < minimum and temp >= 0: minimum = temp if minimum == 999999999: memo[amount] = -1 return memo[amount] memo[amount] = 1 + minimum return memo[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN 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: def coinChange(self, coins, amount): if not coins: return -1 if not amount: return 0 if amount in coins: return 1 C = tuple(sorted(coins, reverse=True)) length, self.ans = len(C) - 1, float("inf") def find(coin, step, target): now_coin = C[coin] q, r = divmod(target, now_coin) if not r: self.ans = min(self.ans, step + q) elif coin < length and step + q + 1 < self.ans: coin += 1 for j in range(q, -1, -1): find(coin, step + j, target - j * now_coin) find(0, 0, amount) return -1 if self.ans == float("inf") else self.ans
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR RETURN VAR FUNC_CALL VAR STRING 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: self.coins = coins self.amount_dict = {(0): 0} for coin in coins: self.amount_dict[coin] = 1 return self.coinChangeHelp(amount) @lru_cache def coinChangeHelp(self, amount: int) -> int: if amount < 0: return -1 if amount in self.amount_dict: return self.amount_dict[amount] max_coin = amount + 1 for coin in self.coins: cur_coin = self.coinChangeHelp(amount - coin) if cur_coin >= 0: max_coin = min(max_coin, cur_coin + 1) max_coin = max_coin if max_coin != amount + 1 else -1 self.amount_dict[amount] = max_coin return max_coin
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER 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: MAX = amount + 1 coins.sort(reverse=True) dp = [MAX] * MAX dp[0] = 0 for i in range(1, MAX): dp[i] = min([(dp[i - c] if i >= c else MAX) for c in coins]) dp[i] = dp[i] + 1 if dp[i] != MAX else dp[i] return -1 if dp[-1] == MAX else dp[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER 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: memo = {} def dfs(amount, path): if amount < 0: return float("inf") if amount == 0: return 0 if amount in memo: return memo[amount] ans = float("inf") for i in coins: r = dfs(amount - i, path + 1) if r != float("inf"): ans = min(ans, r + 1) memo[amount] = ans return ans ret = dfs(amount, 0) return ret if ret != float("inf") else -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR STRING 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 BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR 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
import sys class Solution: def helper(self, coin_sum, coins, memo): if coin_sum == 0: return 0 if coin_sum < 0: return sys.maxsize if memo[coin_sum - 1] != 0: return memo[coin_sum - 1] res = sys.maxsize for c in coins: res = min(res, self.helper(coin_sum - c, coins, memo) + 1) memo[coin_sum - 1] = res return res def coinChange(self, coins, amount): memo = [0] * amount res = self.helper(amount, coins, memo) return -1 if res == sys.maxsize else res
IMPORT CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN 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 coins.sort() for i in range(amount + 1): for j in range(len(coins)): if coins[j] <= amount: dp[i] = min(dp[i], 1 + dp[i - coins[j]]) else: break if dp[amount] > amount: return -1 else: return dp[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 EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR 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(object): def __init__(self): self.mem = {(0): 0} def getMinCoins(self, coins, amount): if amount in self.mem: return self.mem[amount] minCoins = float("inf") for c in coins: if amount - c < 0: break numCoins = self.getMinCoins(coins, amount - c) + 1 minCoins = min(numCoins, minCoins) self.mem[amount] = minCoins return minCoins def coinChange(self, coins, amount): minCoins = self.getMinCoins(sorted(coins), amount) if minCoins == float("inf"): return -1 return minCoins
CLASS_DEF VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER 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: coins = set(coins) dp = [(float("inf") if i not in coins else 1) for i in range(amount + 1)] result = self.helper(coins, amount, dp) return -1 if result == float("inf") else result def helper(self, coins, amount, dp): if amount < 0: return -1 if amount == 0: return 0 if dp[amount] != float("inf"): return dp[amount] current_min = float("inf") for coin in coins: result = self.helper(coins, amount - coin, dp) if result != -1: current_min = min(current_min, 1 + result) if current_min == float("inf"): dp[amount] = -1 return -1 dp[amount] = current_min return current_min
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR FUNC_DEF 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 BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER RETURN 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, amount): if amount < 1: return 0 def count_change(coins, amount, dic={}): if amount < 0: return -1 if amount == 0: return 0 if amount in dic: return dic[amount] minimum = float("inf") for i in range(len(coins)): current = count_change(coins, amount - coins[i], dic) if current >= 0 and current < minimum: minimum = 1 + current if minimum == float("inf"): dic[amount] = -1 else: dic[amount] = minimum return dic[amount] return count_change(coins, amount, {})
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER FUNC_DEF DICT IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR 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 VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR DICT
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 _ in range(amount + 1)] dp[0] = 0 for x in range(len(dp)): for coin in coins: if x < coin: continue if dp[x - coin] + 1 < dp[x]: dp[x] = dp[x - coin] + 1 if dp[amount] == float("inf"): return -1 else: return dp[x]
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 FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER 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 not amount: return 0 coins.sort() coins_set = set(coins) if amount in coins_set: return 1 queue = deque([[amount, 0]]) seen = set() processed = set() while queue: rem, count = queue.popleft() if rem in seen: continue seen.add(rem) for coin in coins: new_rem = rem - coin if new_rem == 0: return count + 1 if new_rem > 0: if new_rem in coins_set: return count + 2 if new_rem not in seen and new_rem not in processed: queue.append([new_rem, count + 1]) processed.add(new_rem) return -1
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST 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(self, coins: List[int], amount: int) -> int: state = [float("inf")] * amount if amount < 1: return 0 max_coin = 0 for c in coins: max_coin = max(c, max_coin) if c <= amount: state[c - 1] = 1 for i in range(amount): for c in coins: if i - c >= 0: state[i] = min(state[i - c] + 1, state[i]) return state[-1] if state[-1] != float("inf") else -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER 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 coinChange(self, coins: List[int], amount: int) -> int: m = [(-1) for x in range(amount + 1)] m[0] = 0 coins = sorted(coins) if amount < min(coins): return m[amount] m[coins[0]] = 1 for x in range(coins[0], amount + 1): current_min = 10000000 for y in coins: if y > x: continue if m[x - y] < current_min and m[x - y] != -1: current_min = m[x - y] if current_min == 10000000: m[x] = -1 else: m[x] = current_min + 1 print(m) return m[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP 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: memory = {} return self._helper(coins, amount, memory) def _helper(self, coins, amount, memory): if amount < 0: return -1 elif amount == 0: return 0 elif amount in memory: return memory[amount] potential_values = [] for coin in coins: new_target = amount - coin min_coins = self._helper(coins, new_target, memory) if min_coins != -1: potential_values.append(min_coins) if len(potential_values) == 0: memory[amount] = -1 else: memory[amount] = min(potential_values) + 1 return memory[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR 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: dp = {} def dfs(total): if total < 0: return float("inf") if total == 0: return 0 if total in list(dp.keys()): return dp[total] for c in coins: x = total - c if total not in list(dp.keys()): dp[total] = dfs(x) + 1 else: dp[total] = min(dp[total], dfs(x) + 1) return dp[total] ans = dfs(amount) if ans < float("inf"): return ans else: return -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING 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: dp = [amount + 1] * (amount + 1) dp[0] = 0 for i in range(1, amount + 1): curr_min = amount + 1 for coin in coins: cand = amount + 1 if i - coin >= 0: cand = dp[i - coin] + 1 if cand < curr_min: curr_min = cand dp[i] = curr_min return -1 if dp[len(dp) - 1] == amount + 1 else dp[len(dp) - 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 BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP 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: dp = [0] * (amount + 1) for i in range(1, len(dp)): dp[i] = float("inf") for j in coins: if i >= j and dp[i - j] + 1 < dp[i]: dp[i] = dp[i - j] + 1 return -1 if dp[amount] == float("inf") else dp[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR STRING 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: if amount == 0: return 0 seen = set() seen.add(0) queue = deque([0]) count = 0 while queue: if amount in queue: return count currlen = len(queue) for i in range(currlen): node = queue.popleft() for c in coins: if c + node not in seen and c + node <= amount: queue.append(c + node) seen.add(c + node) count += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR 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, coins: List[int], amount: int) -> int: dp = [None for i in range(0, amount + 1)] dp[0] = 0 for i in range(1, amount + 1): min_cost = float("inf") for j in range(0, len(coins)): if coins[j] <= i: min_cost = min(min_cost, dp[i - coins[j]] + 1) dp[i] = min_cost if dp[-1] > amount: return -1 return dp[-1] 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 NONE VAR FUNC_CALL VAR NUMBER 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 NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR RETURN NUMBER RETURN VAR NUMBER 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: l = [amount + 1] * (amount + 1) for i in range(len(l)): if i == 0: l[i] = 0 elif all(i < c for c in coins): l[i] = -1 else: for j in range(len(coins)): if i >= coins[j]: num = i - coins[j] if num > 0 and l[num] == -1: if l[i] != amount + 1: continue else: l[i] = -1 else: comb = l[num] + 1 if l[i] == -1: l[i] = amount + 1 if l[i] >= comb: l[i] = comb return l[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR 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
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: amount_list = [i for i in range(amount + 1)] for i in range(amount + 1): if i == 0: amount_list[i] = 0 elif i in coins: amount_list[i] = 1 else: temp = [] for coin in coins: if amount_list[i] - coin >= 0: remainder = amount_list[i] - coin min_coin = amount_list[remainder] if min_coin != -1: temp.append(min_coin) if len(temp) == 0: amount_list[i] = -1 else: amount_list[i] = min(temp) + 1 return amount_list[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER 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: self.memo = {(0): 0} coins.sort() self.getMinCoins(coins, amount) if self.memo[amount] == float("inf"): return -1 return self.memo[amount] def getMinCoins(self, coins, amount): if amount in self.memo: return self.memo[amount] minCoins = float("inf") for coin in coins: if amount - coin < 0: break currCoins = self.getMinCoins(coins, amount - coin) + 1 minCoins = min(minCoins, currCoins) self.memo[amount] = minCoins return self.memo[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR 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: coins.sort(reverse=True) INVALID = 10**10 self.ans = INVALID def dfs(s, amount, count): if amount == 0: self.ans = count return if s == len(coins): return coin = coins[s] for i in range(amount // coin, -1, -1): print(i) print(coin) print(amount) if count + i >= self.ans: break dfs(s + 1, amount - i * coin, count + i) dfs(0, amount, 0) if self.ans == INVALID: return -1 else: return self.ans
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR RETURN IF VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER 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: coins.sort() cache = {} def rec(amount): if amount == 0: return 0 if amount in cache: return cache[amount] val = float("inf") for coin in coins: if amount - coin >= 0: val = min(val, 1 + rec(amount - coin)) cache[amount] = val return val ans = rec(amount) return ans if ans != float("inf") else -1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR 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 amount < 0 or not coins: return -1 if not amount: return 0 stack, lvl, visited = [0], 0, set() while stack: new_lvl = [] lvl += 1 for i in stack: for c in coins: new = i + c if new == amount: return lvl if new not in visited: visited.add(new) new_lvl.append(new) if min(new_lvl) > amount: return -1 stack = new_lvl
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER VAR RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER FUNC_CALL VAR WHILE VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN 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: MAX = amount + 10 dp = [MAX for i in range(amount + 1)] dp[0] = 0 for i in range(1, amount + 1): curr = i for coin in coins: prev = curr - coin if prev >= 0: dp[curr] = min(dp[curr], dp[prev] + 1) return dp[amount] if dp[amount] != MAX else -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER 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 VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR 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: dp = [-1] * (amount + 1) dp[0] = 0 for i in range(1, amount + 1): for coin in coins[::-1]: j = i - coin if j < 0 or dp[j] == -1: continue if dp[i] == -1: dp[i] = dp[j] + 1 else: dp[i] = min(dp[i], dp[j] + 1) return dp[amount]
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 NUMBER BIN_OP VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP 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: dp = [amount + 1] * (amount + 1) dp[0] = 0 coins_set = set(coins) for i in range(amount + 1): if i in coins_set: dp[i] = 1 continue candidates = [(dp[i - c] + 1) for c in coins if i - c >= 0] if candidates: dp[i] = min(candidates) return dp[amount] if dp[amount] <= amount 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 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 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, amount): n = len(coins) dp = [[math.inf for _ in range(amount + 1)] for _ in range(n)] for i, coin in enumerate(coins): for t in range(amount + 1): if t == 0: dp[i][t] = 0 if i > 0: dp[i][t] = dp[i - 1][t] if t >= coin: dp[i][t] = min(dp[i][t], dp[i][t - coin] + 1) return -1 if dp[n - 1][amount] == math.inf else dp[n - 1][amount]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR 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 RETURN VAR BIN_OP VAR NUMBER VAR VAR NUMBER 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_sorted = list(sorted(coins, reverse=True)) memo = {} def choose(amount): if amount in memo: return memo[amount] if amount < 0: return -1 if amount == 0: return 0 memo[amount] = -1 for c in coins_sorted: if (ret := choose(amount - c)) >= 0: if memo[amount] != -1: memo[amount] = min(memo[amount], ret + 1) else: memo[amount] = ret + 1 return memo[amount] return choose(amount)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP 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: N = [(0) for i in range(amount + 1)] N[0] = 0 for i in range(1, amount + 1): temp = [(i - j) for j in coins] history = [ N[temp2] for temp2 in temp if temp2 <= i and temp2 >= 0 and N[temp2] != -1 ] if len(history) == 0: N[i] = -1 else: N[i] = 1 + min( [ N[temp2] for temp2 in temp if temp2 <= i and temp2 >= 0 and N[temp2] != -1 ] ) return N[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 FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER 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 == 0: return 0 coins.sort(reverse=True) memo = {} def dfs(step, rest_amount): if rest_amount < 0: return -1 if rest_amount == 0: return 0 if rest_amount in memo: return memo[rest_amount] min_steps = math.inf for coin in coins: rest = rest_amount - coin res = dfs(step + 1, rest) if res >= 0: min_steps = min(min_steps, res) memo[rest_amount] = min_steps + 1 if min_steps != math.inf else -1 return memo[rest_amount] res = dfs(0, amount) return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL 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: if amount == 0: return 0 q = [0] visited = set() depth = 0 while q: depth += 1 level = [] for curr in q: for coin in coins: newvalue = curr + coin if newvalue == amount: return depth if newvalue not in visited and newvalue < amount: visited.add(newvalue) level.append(newvalue) q = level return -1
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR LIST 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 EXPR FUNC_CALL 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], amount: int) -> int: @lru_cache(maxsize=None) def giveChange(target): if target == 0: return 0 if target < 0: return -1 minForTarget = None for coin in coins: result = giveChange(target - coin) if result != -1: minForTarget = ( result + 1 if not minForTarget else min(result + 1, minForTarget) ) return minForTarget or -1 return giveChange(amount)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER FUNC_CALL VAR NONE 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: matrix = [[(0) for i in range(amount + 1)] for j in range(len(coins) + 1)] for i in range(len(matrix)): matrix[i][0] = 0 for i in range(1, amount + 1): matrix[0][i] = sys.maxsize for i in range(1, len(matrix)): for j in range(len(matrix[0])): if j < coins[i - 1]: matrix[i][j] = matrix[i - 1][j] else: matrix[i][j] = min( matrix[i - 1][j], 1 + matrix[i][j - coins[i - 1]] ) if matrix[-1][-1] == sys.maxsize: return -1 return matrix[-1][-1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR 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 NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR RETURN NUMBER 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 = sorted(coins) memo = {x: (amount + 1) for x in range(amount + 1)} memo[0] = 0 for i in range(amount + 1): for j in range(len(coins)): if coins[j] <= i: memo[i] = min(memo[i], 1 + memo[i - coins[j]]) if memo[amount] < amount + 1: return memo[amount] else: return -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP 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, coin: List[int], sum1: int) -> int: maxv = float("inf") - 1 n = len(coin) t = [[(0) for j in range(sum1 + 1)] for i in range(n + 1)] for i in range(n + 1): for j in range(sum1 + 1): if i == 0 and j == 0: t[i][j] = maxv elif j == 0: t[i][j] = 0 elif i == 0: t[i][j] = maxv for i in range(1, n + 1): for j in range(1, sum1 + 1): if coin[i - 1] <= j: t[i][j] = min(t[i][j - coin[i - 1]] + 1, t[i - 1][j]) else: t[i][j] = t[i - 1][j] if t[n][sum1] == float("inf"): return -1 return t[n][sum1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN 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) min_coins = float("inf") def count_coins(start_coin, coin_count, remaining_amount): nonlocal min_coins if remaining_amount == 0: min_coins = min(min_coins, coin_count) return for i in range(start_coin, len(coins)): remaining_coin_allowance = min_coins - coin_count max_amount_possible = coins[i] * remaining_coin_allowance if ( coins[i] <= remaining_amount and remaining_amount < max_amount_possible ): count_coins(i, coin_count + 1, remaining_amount - coins[i]) count_coins(0, 0, amount) return min_coins if min_coins < float("inf") else -1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR 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: n = len(coins) dp = [[(0) for j in range(amount + 1)] for i in range(n)] for j in range(amount + 1): dp[0][j] = -1 if j % coins[0] else j // coins[0] for i in range(1, n): for j in range(1, amount + 1): includeDenom = -1 if coins[i] > j else dp[i][j - coins[i]] excludeDenom = dp[i - 1][j] if includeDenom == -1 and excludeDenom == -1: dp[i][j] = -1 elif includeDenom == -1: dp[i][j] = excludeDenom elif excludeDenom == -1: dp[i][j] = includeDenom + 1 else: dp[i][j] = min(includeDenom + 1, excludeDenom) return dp[n - 1][amount]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP 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: if amount == 0: return 0 if len(coins) == 1: if amount % coins[0] == 0: return amount // coins[0] return -1 dp = [0] * (amount + 1) for i in range(1, amount + 1): if i in set(coins): dp[i] = 1 continue min_coins = float("inf") for coin in coins: if i - coin >= 0: min_coins = min(dp[i - coin], min_coins) dp[i] = min_coins + 1 if dp[amount] == float("inf"): return -1 return dp[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER 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 == 0: return 0 self.count = [float("inf")] * (amount + 1) self.count[0] = 0 for i in range(1, amount + 1): for coin in coins: rest = i - coin if rest >= 0 and self.count[rest] != float("inf"): self.count[i] = min(1 + self.count[rest], self.count[i]) if self.count[-1] == float("inf"): return -1 else: return self.count[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER 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 FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR 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_mat = [-1] * (amount + 1) dp_mat[0] = 0 for i in range(1, amount + 1): min_num = None for denom in coins: if denom <= i and dp_mat[i - denom] != -1: temp = 1 + dp_mat[i - denom] if not min_num: min_num = temp elif temp < min_num: min_num = temp if min_num: dp_mat[i] = min_num return dp_mat[-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 NUMBER BIN_OP VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR 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
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: queue = deque() numCoins = 0 seen = set() coins.sort(reverse=True) queue.append(amount) if amount == 0: return 0 while queue: qlen = len(queue) numCoins += 1 for i in range(qlen): x = queue.popleft() for coin in coins: if x - coin == 0: return numCoins elif x - coin > 0: if x - coin not in seen: queue.append(x - coin) seen.add(x - coin) return -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN 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 IF BIN_OP VAR VAR NUMBER RETURN VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP 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: def opt(v): if v == 0: return 0 if v in memo: return memo[v] result = float("inf") for i in coins: if v - i >= 0: result = min(result, opt(v - i) + 1) else: continue memo[v] = result return memo[v] memo = {} coins = opt(amount) if coins == float("inf"): return -1 else: return coins
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL 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: if amount == 0: return 0 res, seen, curr = 0, set(), {c for c in coins if c <= amount} while curr: res += 1 if amount in curr: return res seen |= curr tmp = {(n + c) for n in curr for c in coins} curr = {t for t in tmp if t not in seen and t <= amount} return -1
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR WHILE VAR VAR NUMBER IF VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR 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 __init__(self): self.cache = dict() self.coin_set = set() def helper(self, coins: List[int], amount: int) -> int: if amount == 0: return 0 if amount in self.cache: return self.cache[amount] if amount in self.coin_set: return 1 potentials = [float("inf")] for coin in coins: if amount > coin: potentials.append(self.helper(coins, amount - coin)) res = 1 + min(potentials) self.cache[amount] = res return res def coinChange(self, coins: List[int], amount: int) -> int: self.coin_set = set(coins) coins = sorted(coins, reverse=True) res = self.helper(coins, amount) if res == float("inf"): return -1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL 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 __init__(self): self.counts = {} def coinChange(self, coins: List[int], amount: int) -> int: if amount < 0: return -1 if amount == 0: return 0 if amount in self.counts: return self.counts[amount] minCoins = -1 for val in coins: numCoins = self.coinChange(coins, amount - val) if numCoins != -1: if minCoins == -1: minCoins = 1 + numCoins else: minCoins = min(minCoins, 1 + numCoins) self.counts[amount] = minCoins return minCoins
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR VAR 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 NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR 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: coins.sort(reverse=True) count = 0 self.maxcount = float("inf") self.dfs(coins, count, amount) return -1 if self.maxcount == float("inf") else self.maxcount def dfs(self, coins, count, amount): if amount == 0: self.maxcount = min(self.maxcount, count) for i in range(len(coins)): if ( amount >= coins[i] and count + math.ceil(amount / coins[i]) < self.maxcount ): self.dfs(coins[i:], count + 1, amount - coins[i])
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP 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 coins or amount < 0: return -1 f = [sys.maxsize for i in range(amount + 1)] f[0] = 0 for i in range(1, len(f)): for coin in coins: if i - coin >= 0 and f[i - coin] != sys.maxsize: f[i] = min(f[i], f[i - coin] + 1) if f[amount] == sys.maxsize: return -1 return f[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR 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 VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP 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: seen = {(0): 0} def helper(coins, amount): if amount in seen: return seen[amount] if amount < 0: return -1 leastCoins = float("Inf") for coin in coins: next_least = helper(coins, amount - coin) if next_least != -1: leastCoins = min(leastCoins, 1 + next_least) if leastCoins == float("Inf"): seen[amount] = -1 return -1 else: seen[amount] = leastCoins return leastCoins return helper(coins, amount)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR RETURN 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: def helpCoins(coins, amount, cache): if amount < 0: return -1 if amount == 0: return 0 if amount in cache: return cache[amount] curr_min = 999 for coin in coins: to_go = helpCoins(coins, amount - coin, cache) if to_go >= 0 and to_go < curr_min: curr_min = to_go + 1 if curr_min == 999: cache[amount] = -1 else: cache[amount] = curr_min return cache[amount] return helpCoins(coins, amount, {})
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 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 IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN 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: INVALID = 2**32 dp = [INVALID] * (amount + 1) dp[0] = 0 for coin in coins: for i in range(coin, amount + 1): if dp[i - coin] >= dp[i]: continue dp[i] = dp[i - coin] + 1 return -1 if dp[amount] == INVALID else dp[amount]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR 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 __init__(self): self.count = [] def coinChange(self, coins: List[int], amount: int) -> int: if len(self.count) < amount: self.count = [-1] * (amount + 1) self.skip = [False] * (amount + 1) if amount is 0: return 0 elif amount < 0: return -1 else: if self.count[amount] < 0 and not self.skip[amount]: tmp = float("inf") for co in coins: previous = self.coinChange(coins, amount - co) if previous >= 0: self.count[amount] = min(previous + 1, tmp) tmp = self.count[amount] else: continue self.skip[amount] = True return self.count[amount]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN 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: self.dp = [float("inf")] * (amount + 1) self.dp[0] = 0 def change(a): if a < 0: return -1 if a == 0: return 0 if self.dp[a] != float("inf"): return self.dp[a] for c in coins: tmp = change(a - c) if tmp != -1: self.dp[a] = min(tmp + 1, self.dp[a]) if self.dp[a] == float("inf"): self.dp[a] = -1 return self.dp[a] change(amount) return self.dp[amount] if self.dp[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 FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL 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 coinChange(self, coins: List[int], amount: int) -> int: def dp(t): if t in d: return d[t] res = float("inf") for c in coins: if t - c >= 0: res = min(res, dp(t - c) + 1) d[t] = res return res d = {(0): 0} x = dp(amount) return x if x < float("inf") else -1
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR DICT NUMBER NUMBER 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: queue = deque([]) seen = set() coins = sorted(coins, reverse=True) queue.append((0, 0)) while queue: summ, num_coins = queue.popleft() if summ == amount: return num_coins for coin in coins: if summ + coin <= amount and summ + coin not in seen: queue.append((summ + coin, num_coins + 1)) seen.add(summ + coin) return -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR RETURN VAR FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER VAR
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The following sequence is not arithmetic. 1, 1, 2, 5, 7 A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 A slice (P, Q) of array A is called arithmetic if the sequence: A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. The function should return the number of arithmetic slices in the array A. Example: A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
class Solution: def numberOfArithmeticSlices(self, A): dp, counts = [0, 0], 1 for i in range(2, len(A)): if A[i] - A[i - 1] == A[i - 1] - A[i - 2]: dp.append(dp[i - 1] + counts) counts += 1 else: dp.append(dp[i - 1]) counts = 1 return dp[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR NUMBER
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The following sequence is not arithmetic. 1, 1, 2, 5, 7 A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 A slice (P, Q) of array A is called arithmetic if the sequence: A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. The function should return the number of arithmetic slices in the array A. Example: A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
class Solution: def sums(self, n): return int((n - 2) * (n - 1) / 2) def numberOfArithmeticSlices(self, A): if len(A) < 3: return 0 res = [] cnt = 2 diff = 0 i = 1 while i < len(A) - 1: if i == 1: diff = A[i] - A[0] while i < len(A) - 1 and A[i + 1] - A[i] == diff: cnt += 1 i += 1 if cnt >= 3: res.append(cnt) if i < len(A) - 1: diff = A[i + 1] - A[i] cnt = 2 i += 1 return sum(self.sums(x) for x in res)
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The following sequence is not arithmetic. 1, 1, 2, 5, 7 A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 A slice (P, Q) of array A is called arithmetic if the sequence: A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. The function should return the number of arithmetic slices in the array A. Example: A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
class Solution: def __init__(self): self.total_sum = 0 def numberOfArithmeticSlices(self, A): self.slices(A, len(A) - 1) return self.total_sum def slices(self, A, i): if i < 2: return 0 temp = 0 if A[i] - A[i - 1] == A[i - 1] - A[i - 2]: temp = 1 + self.slices(A, i - 1) self.total_sum += temp else: self.slices(A, i - 1) return temp
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The following sequence is not arithmetic. 1, 1, 2, 5, 7 A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 A slice (P, Q) of array A is called arithmetic if the sequence: A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. The function should return the number of arithmetic slices in the array A. Example: A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
class Solution: def numberOfArithmeticSlices(self, A): size = len(A) if size < 3: return 0 ans = 0 cnt = 0 delta = A[1] - A[0] for x in range(2, size): if A[x] - A[x - 1] == delta: cnt += 1 ans += cnt else: delta = A[x] - A[x - 1] cnt = 0 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The following sequence is not arithmetic. 1, 1, 2, 5, 7 A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 A slice (P, Q) of array A is called arithmetic if the sequence: A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. The function should return the number of arithmetic slices in the array A. Example: A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
class Solution: def numberOfArithmeticSlices(self, A): count = 0 addend = 0 for i in range(2, len(A)): if A[i - 1] - A[i] == A[i - 2] - A[i - 1]: addend += 1 count += addend else: addend = 0 return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER RETURN VAR
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The following sequence is not arithmetic. 1, 1, 2, 5, 7 A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 A slice (P, Q) of array A is called arithmetic if the sequence: A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. The function should return the number of arithmetic slices in the array A. Example: A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
class Solution: def numberOfArithmeticSlices(self, A): ans = [0] * len(A) for i in range(0, len(A)): if i < 2: ans[i] = 0 else: ans[i] = ans[i - 1] temp = A[: i + 1][::-1] for j in range(0, i - 1): slice = temp[j : j + 3] if self.checkArithmetic(slice): ans[i] += 1 else: break return 0 if len(A) == 0 else ans[-1] def checkArithmetic(self, slice): diff = slice[1] - slice[0] for i in range(len(slice) - 1): if slice[i + 1] - slice[i] != diff: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN NUMBER
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The following sequence is not arithmetic. 1, 1, 2, 5, 7 A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 A slice (P, Q) of array A is called arithmetic if the sequence: A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. The function should return the number of arithmetic slices in the array A. Example: A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
class Solution: def numberOfArithmeticSlices(self, A): if len(A) < 3: return 0 count = 0 l = len(A) i = 0 while i < l - 2: j = i + 2 while j < l: if A[i] - A[i + 1] == A[j - 1] - A[j]: count += j - i - 1 j += 1 else: i = j - 1 break if j == l: break return count
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The following sequence is not arithmetic. 1, 1, 2, 5, 7 A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 A slice (P, Q) of array A is called arithmetic if the sequence: A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. The function should return the number of arithmetic slices in the array A. Example: A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
class Solution: def numberOfArithmeticSlices(self, A): res = 0 dp = [0] * len(A) for i in range(2, len(A)): if A[i] - A[i - 1] == A[i - 1] - A[i - 2]: dp[i] = dp[i - 1] + 1 res += dp[i] return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR RETURN VAR
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains. For example, this picture describes the chronological order of games with k = 3: <image> Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows: * if s_i is 0, then the team with lower index wins the i-th game; * if s_i is 1, then the team with greater index wins the i-th game; * if s_i is ?, then the result of the i-th game is unknown (any team could win this game). Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion. You are given the initial state of the string s. You have to process q queries of the following form: * p c — replace s_p with character c, and print f(s) as the result of the query. Input The first line contains one integer k (1 ≤ k ≤ 18). The second line contains a string consisting of 2^k - 1 characters — the initial state of the string s. Each character is either ?, 0, or 1. The third line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries. Then q lines follow, the i-th line contains an integer p and a character c (1 ≤ p ≤ 2^k - 1; c is either ?, 0, or 1), describing the i-th query. Output For each query, print one integer — f(s). Example Input 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 Output 1 2 3 3 5 4
import sys input = sys.stdin.readline n = int(input()) s = list(input())[::-1] s = s[1:] temp = [(1) for i in range(2000000)] for ind in range((1 << n) - 2, -1, -1): if s[ind] == "0": temp[ind] = temp[2 * ind + 2] elif s[ind] == "1": temp[ind] = temp[2 * ind + 1] else: temp[ind] = temp[2 * ind + 1] + temp[2 * ind + 2] for _ in range(int(input())): a, b = [x for x in input().split()] a = int(a) - 1 a = (1 << n) - 2 - a s[a] = b ind = a while ind >= 0: if s[ind] == "0": temp[ind] = temp[2 * ind + 2] elif s[ind] == "1": temp[ind] = temp[2 * ind + 1] else: temp[ind] = temp[2 * ind + 1] + temp[2 * ind + 2] if ind == 0: break ind = (ind - 1) // 2 print(temp[0])
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains. For example, this picture describes the chronological order of games with k = 3: <image> Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows: * if s_i is 0, then the team with lower index wins the i-th game; * if s_i is 1, then the team with greater index wins the i-th game; * if s_i is ?, then the result of the i-th game is unknown (any team could win this game). Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion. You are given the initial state of the string s. You have to process q queries of the following form: * p c — replace s_p with character c, and print f(s) as the result of the query. Input The first line contains one integer k (1 ≤ k ≤ 18). The second line contains a string consisting of 2^k - 1 characters — the initial state of the string s. Each character is either ?, 0, or 1. The third line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries. Then q lines follow, the i-th line contains an integer p and a character c (1 ≤ p ≤ 2^k - 1; c is either ?, 0, or 1), describing the i-th query. Output For each query, print one integer — f(s). Example Input 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 Output 1 2 3 3 5 4
import sys def main(): k = int(input()) s = input() n = len(s) arr = [-1] * (n + 1) j = 1 for i in range(n - 1, -1, -1): arr[j] = s[i] j += 1 n += 1 tree = [0] * n for i in range(n - 1, 0, -1): if i * 2 < n: if arr[i] == "?": tree[i] = tree[i * 2] + tree[i * 2 + 1] elif arr[i] == "0": tree[i] = tree[i * 2 + 1] else: tree[i] = tree[i * 2] elif arr[i] == "?": tree[i] = 2 else: tree[i] = 1 q = int(input()) allans = [] for _ in range(q): p, c = input().split() i = n - int(p) arr[i] = c while i > 0: if i * 2 < n: if arr[i] == "?": tree[i] = tree[i * 2] + tree[i * 2 + 1] elif arr[i] == "0": tree[i] = tree[i * 2 + 1] else: tree[i] = tree[i * 2] elif arr[i] == "?": tree[i] = 2 else: tree[i] = 1 i //= 2 ans = tree[1] allans.append(ans) multiLineArrayPrint(allans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(defaultValFactory, dimensionArr): dv = defaultValFactory da = dimensionArr if len(da) == 1: return [dv() for _ in range(da[0])] else: return [makeArr(dv, da[1:]) for _ in range(da[0])] def queryInteractive(i, j): print("? {} {}".format(i, j)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(" ".join([str(x) for x in ans]))) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains. For example, this picture describes the chronological order of games with k = 3: <image> Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows: * if s_i is 0, then the team with lower index wins the i-th game; * if s_i is 1, then the team with greater index wins the i-th game; * if s_i is ?, then the result of the i-th game is unknown (any team could win this game). Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion. You are given the initial state of the string s. You have to process q queries of the following form: * p c — replace s_p with character c, and print f(s) as the result of the query. Input The first line contains one integer k (1 ≤ k ≤ 18). The second line contains a string consisting of 2^k - 1 characters — the initial state of the string s. Each character is either ?, 0, or 1. The third line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries. Then q lines follow, the i-th line contains an integer p and a character c (1 ≤ p ≤ 2^k - 1; c is either ?, 0, or 1), describing the i-th query. Output For each query, print one integer — f(s). Example Input 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 Output 1 2 3 3 5 4
import sys input = sys.stdin.readline def build(curr): l = curr << 1 r = (curr << 1) + 1 if tree[l] == -1: build(l) if tree[r] == -1: build(r) if s[curr - 1] == "0": tree[curr] = tree[r] elif s[curr - 1] == "1": tree[curr] = tree[l] else: tree[curr] = tree[l] + tree[r] k = int(input()) s = list(input().strip()) s.reverse() n = len(s) tree = [-1] * (2 * (n + 1) + 2) for i in range(n + 1): tree[n + i + 1] = 1 build(1) q = int(input()) for i in range(q): inp = input().split() ind = int(inp[0]) val = inp[1] s[n - ind] = val curr = n - ind + 1 while curr > 0: l = curr << 1 r = (curr << 1) + 1 if s[curr - 1] == "?": tree[curr] = tree[l] + tree[r] elif s[curr - 1] == "1": tree[curr] = tree[l] else: tree[curr] = tree[r] curr >>= 1 print(tree[1])
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains. For example, this picture describes the chronological order of games with k = 3: <image> Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows: * if s_i is 0, then the team with lower index wins the i-th game; * if s_i is 1, then the team with greater index wins the i-th game; * if s_i is ?, then the result of the i-th game is unknown (any team could win this game). Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion. You are given the initial state of the string s. You have to process q queries of the following form: * p c — replace s_p with character c, and print f(s) as the result of the query. Input The first line contains one integer k (1 ≤ k ≤ 18). The second line contains a string consisting of 2^k - 1 characters — the initial state of the string s. Each character is either ?, 0, or 1. The third line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries. Then q lines follow, the i-th line contains an integer p and a character c (1 ≤ p ≤ 2^k - 1; c is either ?, 0, or 1), describing the i-th query. Output For each query, print one integer — f(s). Example Input 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 Output 1 2 3 3 5 4
import sys input = lambda: sys.stdin.readline().strip() k = int(input()) S = list(input()) N = len(S) dp = [0] * N for i, s in enumerate(S): if i <= N // 2: dp[i] += 2 if s == "?" else 1 else: d = N - i l, r = N - d * 2 - 1, N - d * 2 if s == "?": dp[i] = dp[l] + dp[r] elif s == "1": dp[i] = dp[r] elif s == "0": dp[i] = dp[l] for _ in range(int(input())): i, to = input().split() i = int(i) - 1 S[i] = to while i < N: s = S[i] d = N - i l, r = N - d * 2 - 1, N - d * 2 if i <= N // 2: dp[i] = 2 if s == "?" else 1 elif s == "?": dp[i] = dp[l] + dp[r] elif s == "1": dp[i] = dp[r] elif s == "0": dp[i] = dp[l] d = (N - i) // 2 i = N - d print(dp[-1])
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR STRING NUMBER NUMBER IF VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains. For example, this picture describes the chronological order of games with k = 3: <image> Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows: * if s_i is 0, then the team with lower index wins the i-th game; * if s_i is 1, then the team with greater index wins the i-th game; * if s_i is ?, then the result of the i-th game is unknown (any team could win this game). Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion. You are given the initial state of the string s. You have to process q queries of the following form: * p c — replace s_p with character c, and print f(s) as the result of the query. Input The first line contains one integer k (1 ≤ k ≤ 18). The second line contains a string consisting of 2^k - 1 characters — the initial state of the string s. Each character is either ?, 0, or 1. The third line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries. Then q lines follow, the i-th line contains an integer p and a character c (1 ≤ p ≤ 2^k - 1; c is either ?, 0, or 1), describing the i-th query. Output For each query, print one integer — f(s). Example Input 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 Output 1 2 3 3 5 4
import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LI1(): return list(map(int1, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 10**16 md = 10**9 + 7 k = II() n = 1 << k s = SI() vv = [2] * n tt = [2] * n for i, c in enumerate(s, 1): i = n - i if c == "?": tt[i] = 2 if i >= n // 2: vv[i] = 2 else: vv[i] = vv[i * 2] + vv[i * 2 + 1] else: tt[i] = int(c) if i >= n // 2: vv[i] = 1 else: vv[i] = vv[i * 2 + 1 - int(c)] for _ in range(II()): p, c = SI().split() p = n - int(p) if c == "?": tt[p] = 2 else: tt[p] = int(c) while p: if p >= n // 2: if tt[p] == 2: vv[p] = 2 else: vv[p] = 1 elif tt[p] == 2: vv[p] = vv[p * 2] + vv[p * 2 + 1] else: vv[p] = vv[p * 2 + 1 - tt[p]] p //= 2 print(vv[1])
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR STRING ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR WHILE VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains. For example, this picture describes the chronological order of games with k = 3: <image> Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows: * if s_i is 0, then the team with lower index wins the i-th game; * if s_i is 1, then the team with greater index wins the i-th game; * if s_i is ?, then the result of the i-th game is unknown (any team could win this game). Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion. You are given the initial state of the string s. You have to process q queries of the following form: * p c — replace s_p with character c, and print f(s) as the result of the query. Input The first line contains one integer k (1 ≤ k ≤ 18). The second line contains a string consisting of 2^k - 1 characters — the initial state of the string s. Each character is either ?, 0, or 1. The third line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries. Then q lines follow, the i-th line contains an integer p and a character c (1 ≤ p ≤ 2^k - 1; c is either ?, 0, or 1), describing the i-th query. Output For each query, print one integer — f(s). Example Input 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 Output 1 2 3 3 5 4
import sys input = sys.stdin.readline K = int(input()) S = input().rstrip() Q = int(input()) PC = [input().split() for i in range(Q)] A = [""] + list(S)[::-1] N = len(A) C = [0] * N for i in range(N // 2, N): if A[i] == "?": C[i] = 2 else: C[i] = 1 for i in range(N // 2 - 1, 0, -1): if A[i] == "?": C[i] = C[i * 2] + C[i * 2 + 1] elif A[i] == "1": C[i] = C[i * 2] else: C[i] = C[i * 2 + 1] ans = [] for p, c in PC: i = N - int(p) if c == A[i]: ans.append(C[1]) continue A[i] = c if i >= N // 2: if c == "?": C[i] = 2 else: C[i] = 1 i //= 2 while i >= 1: if A[i] == "?": C[i] = C[i * 2] + C[i * 2 + 1] elif A[i] == "1": C[i] = C[i * 2] else: C[i] = C[i * 2 + 1] i //= 2 ans.append(C[1]) print(*ans, sep="\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains. For example, this picture describes the chronological order of games with k = 3: <image> Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows: * if s_i is 0, then the team with lower index wins the i-th game; * if s_i is 1, then the team with greater index wins the i-th game; * if s_i is ?, then the result of the i-th game is unknown (any team could win this game). Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion. You are given the initial state of the string s. You have to process q queries of the following form: * p c — replace s_p with character c, and print f(s) as the result of the query. Input The first line contains one integer k (1 ≤ k ≤ 18). The second line contains a string consisting of 2^k - 1 characters — the initial state of the string s. Each character is either ?, 0, or 1. The third line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries. Then q lines follow, the i-th line contains an integer p and a character c (1 ≤ p ≤ 2^k - 1; c is either ?, 0, or 1), describing the i-th query. Output For each query, print one integer — f(s). Example Input 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 Output 1 2 3 3 5 4
import sys K = int(input()) two_K = 1 << K S = list(sys.stdin.readline()[:-1]) dp = [1] * (two_K << 1) for i in range(two_K - 1, 0, -1): c = S[two_K - i - 1] if c == "1": dp[i] = dp[i << 1] elif c == "0": dp[i] = dp[(i << 1) + 1] else: dp[i] = dp[i << 1] + dp[(i << 1) + 1] Q = int(input()) ans = [] for _ in range(Q): match_number, winner = sys.stdin.readline().strip().split() match_number = int(match_number) - 1 S[match_number] = winner i = two_K - match_number - 1 while i: c = S[two_K - i - 1] if c == "1": dp[i] = dp[i << 1] elif c == "0": dp[i] = dp[(i << 1) + 1] else: dp[i] = dp[i << 1] + dp[(i << 1) + 1] i //= 2 ans.append(dp[1]) for a in ans: print(a)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains. For example, this picture describes the chronological order of games with k = 3: <image> Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows: * if s_i is 0, then the team with lower index wins the i-th game; * if s_i is 1, then the team with greater index wins the i-th game; * if s_i is ?, then the result of the i-th game is unknown (any team could win this game). Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion. You are given the initial state of the string s. You have to process q queries of the following form: * p c — replace s_p with character c, and print f(s) as the result of the query. Input The first line contains one integer k (1 ≤ k ≤ 18). The second line contains a string consisting of 2^k - 1 characters — the initial state of the string s. Each character is either ?, 0, or 1. The third line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries. Then q lines follow, the i-th line contains an integer p and a character c (1 ≤ p ≤ 2^k - 1; c is either ?, 0, or 1), describing the i-th query. Output For each query, print one integer — f(s). Example Input 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 Output 1 2 3 3 5 4
def solve(): k = int(input()) s = list(input()) q = int(input()) queries = [tuple(input().split()) for _ in range(q)] lim = 2**k seg = [1] * lim seg[0] = 0 for i in range(lim // 2): if s[i] == "?": seg[lim - i - 1] = 2 for i in range(lim // 2, lim - 1): j = lim - i - 1 if s[i] == "?": seg[j] = seg[j * 2] + seg[j * 2 + 1] elif s[i] == "1": seg[j] = seg[j * 2] else: seg[j] = seg[j * 2 + 1] for p, c in queries: s[int(p) - 1] = c j = lim - int(p) if 2 * j + 1 < lim: if c == "?": seg[j] = seg[j * 2] + seg[j * 2 + 1] elif c == "1": seg[j] = seg[j * 2] else: seg[j] = seg[j * 2 + 1] elif c == "?": seg[j] = 2 else: seg[j] = 1 while j // 2 > 0: j = j // 2 i = lim - j - 1 if s[i] == "?": seg[j] = seg[j * 2] + seg[j * 2 + 1] elif s[i] == "1": seg[j] = seg[j * 2] else: seg[j] = seg[j * 2 + 1] print(seg[1]) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains. For example, this picture describes the chronological order of games with k = 3: <image> Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows: * if s_i is 0, then the team with lower index wins the i-th game; * if s_i is 1, then the team with greater index wins the i-th game; * if s_i is ?, then the result of the i-th game is unknown (any team could win this game). Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion. You are given the initial state of the string s. You have to process q queries of the following form: * p c — replace s_p with character c, and print f(s) as the result of the query. Input The first line contains one integer k (1 ≤ k ≤ 18). The second line contains a string consisting of 2^k - 1 characters — the initial state of the string s. Each character is either ?, 0, or 1. The third line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries. Then q lines follow, the i-th line contains an integer p and a character c (1 ≤ p ≤ 2^k - 1; c is either ?, 0, or 1), describing the i-th query. Output For each query, print one integer — f(s). Example Input 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 Output 1 2 3 3 5 4
import sys input = sys.stdin.readline k = int(input()) n = 1 << k s = list(input()[:-1]) dp = [1] * (2 * n) for i in range(n - 1, 0, -1): if s[n - 1 - i] == "0": dp[i] = dp[i * 2 + 1] elif s[n - 1 - i] == "1": dp[i] = dp[i * 2] else: dp[i] = dp[i * 2] + dp[i * 2 + 1] for _ in range(int(input())): p, c = input().split() p = int(p) - 1 s[p] = c i = n - 1 - p while i: if s[n - 1 - i] == "0": dp[i] = dp[i * 2 + 1] elif s[n - 1 - i] == "1": dp[i] = dp[i * 2] else: dp[i] = dp[i * 2] + dp[i * 2 + 1] i //= 2 print(dp[1])
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER