Problema · algoritmi medio Python ·
Ways to make change
Count the ways to make amount from coins of the given distinct positive denominations, each usable any number of times. Order doesn't matter (1+2 and 2+1 are the same way). Return the count modulo 1,000,000,007. There is exactly one way to make 0. Up to 60 denominations and amounts up to 30,000.
Firma
def coin_ways(coins: list[int], amount: int) -> int:Esempi
coin_ways([1, 2, 5], 5) # -> 4
coin_ways([2], 3) # -> 0
coin_ways([10], 10) # -> 1Più 7 test nascosti e 1 test con grandi input con limite di tempo; i relativi input non sono mostrati. I problemi vengono sottoposti ai modelli in inglese.
1° postoMistral Devstral 2 2512risolto · 11 su 11 test superatirisposta in 906 ms · esecuzione 345 ms · $0.0003 ✓ coin_ways([1, 2, 5], 5)atteso 4 2 ms ✓ coin_ways([2], 3)atteso 0 <1 ms ✓ coin_ways([10], 10)atteso 1 <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 15 ms⏱ 322 msCodice
def coin_ways(coins: list[int], amount: int) -> int: MOD = 10**9 + 7 dp = [0] * (amount + 1) dp[0] = 1 for coin in coins: for i in range(coin, amount + 1): dp[i] = (dp[i] + dp[i - coin]) % MOD return dp[amount]1° postoQwen3 Coder Nextrisolto · 11 su 11 test superatirisposta in 2.8 s · esecuzione 352 ms · $0.0002 ✓ coin_ways([1, 2, 5], 5)atteso 4 <1 ms ✓ coin_ways([2], 3)atteso 0 <1 ms ✓ coin_ways([10], 10)atteso 1 2 ms ✓ 1 ms✓ <1 ms✓ 2 ms✓ 3 ms✓ <1 ms✓ <1 ms✓ 14 ms⏱ 327 msCodice
def coin_ways(coins: list[int], amount: int) -> int: MOD = 1000000007 # dp[i] = number of ways to make amount i dp = [0] * (amount + 1) dp[0] = 1 # One way to make 0: use no coins # For each coin, update dp array for all amounts >= coin for coin in coins: for i in range(coin, amount + 1): dp[i] = (dp[i] + dp[i - coin]) % MOD return dp[amount]1° postoSpaceXAI Grok Build 0.1risolto · 11 su 11 test superatirisposta in 5.4 s · esecuzione 354 ms · $0.0022 ✓ coin_ways([1, 2, 5], 5)atteso 4 <1 ms ✓ coin_ways([2], 3)atteso 0 <1 ms ✓ coin_ways([10], 10)atteso 1 <1 ms ✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 14 ms⏱ 334 msCodice
def coin_ways(coins: list[int], amount: int) -> int: MOD = 1000000007 dp = [0] * (amount + 1) dp[0] = 1 for coin in coins: for x in range(coin, amount + 1): dp[x] = (dp[x] + dp[x - coin]) % MOD return dp[amount]1° postoAnthropic Claude Haiku 5.5risolto · 11 su 11 test superatirisposta in 1.7 s · esecuzione 355 ms · $0.0002 ✓ coin_ways([1, 2, 5], 5)atteso 4 <1 ms ✓ coin_ways([2], 3)atteso 0 2 ms ✓ coin_ways([10], 10)atteso 1 <1 ms ✓ <1 ms✓ <1 ms✓ 1 ms✓ 1 ms✓ 1 ms✓ 1 ms✓ 15 ms⏱ 330 msCodice
def coin_ways(coins: list[int], amount: int) -> int: MOD = 1_000_000_007 if amount < 0: return 0 dp = [0] * (amount + 1) dp[0] = 1 for c in coins: if c <= 0 or c > amount: continue for a in range(c, amount + 1): dp[a] = (dp[a] + dp[a - c]) % MOD return dp[amount] % MOD
Costo dei token: $0.0028 · Come funzionano round e punteggi →