0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python】構築不可能なお金

0
Posted at

作るもの

与えられた整数のリスト(コインの額面)を使用して作成できない最小のお金の額を計算する関数。

実装

non-constructible_change.py
def nonConstructibleChange(coins):
    # コインの額面を昇順にソートする
    coins.sort()

    # コインを使って作成できる最小のお金の額を表す変数を初期化
    minimum_change = 0

    # ソートされたコインのリストを順に走査
    for coin in coins:
        # 現在のコインの額面が、最小のお金の額に1を加えたものよりも大きい場合、ループを終了
        if coin > minimum_change + 1:
            break

        # 現在のコインの額面を最小のお金の額に加えることで更新
        minimum_change += coin

    # 最小のお金の額に1を加えた値を返す
    return minimum_change + 1

# テスト
if __name__ == "__main__":
    coins = [6, 4, 5, 1, 1, 8, 9]
    print(nonConstructibleChange(coins))

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?