From 44feaa2478c938bca0d10ddae80883ab384f4bf8 Mon Sep 17 00:00:00 2001 From: Tafara-N Date: Thu, 22 Aug 2024 11:29:20 +0200 Subject: [PATCH] Determining the fewest number of coins needed to meet a given amount total. --- 0x08-making_change/0-making_change.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/0x08-making_change/0-making_change.py b/0x08-making_change/0-making_change.py index 6011398..9a923bd 100755 --- a/0x08-making_change/0-making_change.py +++ b/0x08-making_change/0-making_change.py @@ -19,4 +19,20 @@ def makeChange(coins, total): fewest number of coins needed to meet a given amount """ - pass + if total <= 0: + return 0 + + coins.sort(reverse=True) + num_coins = 0 + + for coin in coins: + if total <= 0: + break + + num_coins += total // coin + total %= coin + + if total != 0: + return -1 + + return num_coins