-
-
Save Evan-Kim2028/2e9c75e1f1d23e184e72b6d177aeb696 to your computer and use it in GitHub Desktop.
other solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A Dynamic Programming based Python3 program to | |
# find minimum of coins to make a given change V | |
import sys | |
# m is size of coins array (number of | |
# different coins) | |
def minCoins(coins, V): | |
# table[i] will be storing the minimum | |
# number of coins required for i value. | |
# So table[V] will have result | |
table = [sys.maxsize for i in range(V + 1)] | |
# Base case (If given value V is 0) | |
table[0] = 0 | |
# Compute minimum coins required | |
# for all values from 1 to V | |
speed_count = 0 | |
for value in range(1, V + 1): | |
speed_count +=1 | |
# Go through all coins smaller than value | |
for coin in coins: # take first element 1 | |
if (coin <= value): #if 1 <= 1 | |
sub_res = table[value - coin] #sub_res = table[1-1] | |
if (sub_res != sys.maxsize and sub_res + 1 < table[value]): #1 (sub_res is 0 + 1) < 0 (table[value] is 0) | |
table[value] = sub_res + 1 # table[value] = 1 | |
speed_count +=1 | |
print("other_speed_count", speed_count) | |
return table[V] | |
# Driver Code | |
if __name__ == "__main__": | |
coin_list = [1,4,5] | |
target = 12 | |
print("Minimum coins required is ", | |
minCoins(coin_list, target)) | |
# This code is contributed by ita_c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment