課題 · アルゴリズム 中級 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.
シグネチャ
def coin_ways(coins: list[int], amount: int) -> int:入力例
coin_ways([1, 2, 5], 5) # -> 4
coin_ways([2], 3) # -> 0
coin_ways([10], 10) # -> 1その他、入力値が非公開の7個の非公開テストおよび制限時間付きの1個の大規模入力テストがあります。 モデルへの課題は英語で出題されます。
#1Mistral Devstral 2 2512正解 · 11 / 11 件のテストにパス回答時間 906 ms · 実行時間 345 ms · $0.0003 ✓ coin_ways([1, 2, 5], 5)期待値 4 2 ms ✓ coin_ways([2], 3)期待値 0 <1 ms ✓ coin_ways([10], 10)期待値 1 <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 15 ms⏱ 322 msコード
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正解 · 11 / 11 件のテストにパス回答時間 2.8 秒 · 実行時間 352 ms · $0.0002 ✓ coin_ways([1, 2, 5], 5)期待値 4 <1 ms ✓ coin_ways([2], 3)期待値 0 <1 ms ✓ coin_ways([10], 10)期待値 1 2 ms ✓ 1 ms✓ <1 ms✓ 2 ms✓ 3 ms✓ <1 ms✓ <1 ms✓ 14 ms⏱ 327 msコード
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正解 · 11 / 11 件のテストにパス回答時間 5.4 秒 · 実行時間 354 ms · $0.0022 ✓ coin_ways([1, 2, 5], 5)期待値 4 <1 ms ✓ coin_ways([2], 3)期待値 0 <1 ms ✓ coin_ways([10], 10)期待値 1 <1 ms ✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 14 ms⏱ 334 msコード
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正解 · 11 / 11 件のテストにパス回答時間 1.7 秒 · 実行時間 355 ms · $0.0002 ✓ coin_ways([1, 2, 5], 5)期待値 4 <1 ms ✓ coin_ways([2], 3)期待値 0 2 ms ✓ coin_ways([10], 10)期待値 1 <1 ms ✓ <1 ms✓ <1 ms✓ 1 ms✓ 1 ms✓ 1 ms✓ 1 ms✓ 15 ms⏱ 330 msコード
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
トークンコスト: $0.0028 · 対戦の進行と評価方法について →