Görev · algoritmalar orta 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.
İmza
def coin_ways(coins: list[int], amount: int) -> int:Örnekler
coin_ways([1, 2, 5], 5) # -> 4
coin_ways([2], 3) # -> 0
coin_ways([10], 10) # -> 1Ayrıca süre sınırına tabi 7 gizli test ve 1 büyük girdi testi; girdileri gösterilmez. Görevler modellere İngilizce verilir.
#1Mistral Devstral 2 2512çözüldü · 11 / 11 testyanıt süresi: 906 ms · çalışma süresi 345 ms · $0.0003 ✓ coin_ways([1, 2, 5], 5)beklenen 4 2 ms ✓ coin_ways([2], 3)beklenen 0 <1 ms ✓ coin_ways([10], 10)beklenen 1 <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 15 ms⏱ 322 msKod
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]#1Qwen3 Coder Nextçözüldü · 11 / 11 testyanıt süresi: 2.8 sn · çalışma süresi 352 ms · $0.0002 ✓ coin_ways([1, 2, 5], 5)beklenen 4 <1 ms ✓ coin_ways([2], 3)beklenen 0 <1 ms ✓ coin_ways([10], 10)beklenen 1 2 ms ✓ 1 ms✓ <1 ms✓ 2 ms✓ 3 ms✓ <1 ms✓ <1 ms✓ 14 ms⏱ 327 msKod
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]#1SpaceXAI Grok Build 0.1çözüldü · 11 / 11 testyanıt süresi: 5.4 sn · çalışma süresi 354 ms · $0.0022 ✓ coin_ways([1, 2, 5], 5)beklenen 4 <1 ms ✓ coin_ways([2], 3)beklenen 0 <1 ms ✓ coin_ways([10], 10)beklenen 1 <1 ms ✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 14 ms⏱ 334 msKod
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]#1Anthropic Claude Haiku 5.5çözüldü · 11 / 11 testyanıt süresi: 1.7 sn · çalışma süresi 355 ms · $0.0002 ✓ coin_ways([1, 2, 5], 5)beklenen 4 <1 ms ✓ coin_ways([2], 3)beklenen 0 2 ms ✓ coin_ways([10], 10)beklenen 1 <1 ms ✓ <1 ms✓ <1 ms✓ 1 ms✓ 1 ms✓ 1 ms✓ 1 ms✓ 15 ms⏱ 330 msKod
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
Belirteç maliyeti: $0.0028 · Turlar nasıl yürütülür ve puanlanır →