min coin change problem dp
C++
// A Dynamic Programming based C++ program to find minimum of coins
// to make a given change V
#include<bits/stdc++.h>
using namespace std;
// m is size of coins array (number of different coins)
int minCoins(int coins[], int m, int V)
{
// table[i] will be storing the minimum number of coins
// required for i value. So table[V] will have result
int table[V+1];
// Base case (If given value V is 0)
table[0] = 0;
// Initialize all table values as Infinite
for (int i=1; i<=V; i++)
table[i] = INT_MAX;
// Compute minimum coins required for all
// values from 1 to V
for (int i=1; i<=V; i++)
{
// Go through all coins smaller than i
for (int j=0; j<m; j++)
if (coins[j] <= i)
{
int sub_res = table[i-coins[j]];
if (sub_res != INT_MAX && sub_res + 1 < table[i])
table[i] = sub_res + 1;
}
}
return table[V];
}
// Driver program to test above function
int main()
{
int coins[] = {9, 6, 5, 1};
int m = sizeof(coins)/sizeof(coins[0]);
int V = 11;
cout << "Minimum coins required is "
<< minCoins(coins, m, V);
return 0;
}
# 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, m, V):
# table[i] will be storing the minimum
# number of coins required for i value.
# So table[V] will have result
table = [0 for i in range(V + 1)]
# Base case (If given value V is 0)
table[0] = 0
# Initialize all table values as Infinite
for i in range(1, V + 1):
table[i] = sys.maxsize
# Compute minimum coins required
# for all values from 1 to V
for i in range(1, V + 1):
# Go through all coins smaller than i
for j in range(m):
if (coins[j] <= i):
sub_res = table[i - coins[j]]
if (sub_res != sys.maxsize and
sub_res + 1 < table[i]):
table[i] = sub_res + 1
return table[V]
# Driver Code
if __name__ == "__main__":
coins = [9, 6, 5, 1]
m = len(coins)
V = 11
print("Minimum coins required is ",
minCoins(coins, m, V))
# This code is contributed by ita_c
Also in C++:
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- C++ If
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- how to print nth palindrome number in c++
- Category
- C++
- Title
- namespace file linking c++
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- nginx linux
- Category
- C++
- Title
- convert char to string - c++
- Category
- C++
- Title
- C++ user input
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- how to read and write in a file c++
- Category
- C++
- Title
- c++ how to use scanf
- Category
- C++
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- -> cpp
- Category
- C++
- Title
- initialization list c++
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- c++ char print fixed
- Category
- C++
- Title
- std::substring
- Category
- C++
- Title
- widechartomultibyte
- Category
- C++
- Title
- ceil in c++
- Category
- C++
- Title
- log base e synthax c++
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- can we compare a long long int with int in c++ using max or min functions
- Category
- C++
- Title
- array sort c++
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- c++ assert
- Category
- C++
- Title
- matrix transpose tiling
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- invalid types int int for array subscript c++
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- how to append to a vector c++
- Category
- C++
- Title
- c++ stream string into fiel
- Category
- C++
- Title
- initialize 2d array c++
- Category
- C++
- Title
- error: ‘memset’ was not declared in this scope in cpp
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- else if c++
- Category
- C++
- Title
- c++ overloaded equality check operator
- Category
- C++
- Title
- c++ program how to let the user choose different game modes
- Category
- C++
- Title
- how to convert number to string
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- sqrt cpp
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- c++ call method in same class
- Category
- C++
- Title
- int random string generator c++
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- setbits
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- traversing map cpp
- Category
- C++
- Title
- dynamic 2d array c++
- Category
- C++
- Title
- add two numbers in c++
- Category
- C++
- Title
- how to allocate on heap in c++
- Category
- C++
- Title
- subtracting two large numbers
- Category
- C++
- Title
- c++ split at character
- Category
- C++
- Title
- c++ rainbow text
- Category
- C++
- Title
- c++ vector constructors
- Category
- C++
- Title
- c++ sort vector of objects by property
- Category
- C++
- Title
- how to add a number after each number in an array with a for loop in C++
- Category
- C++
- Title
- COunt the number of continous subsequences such that the sum is between
- Category
- C++
- Title
- when ratings will be updated for codechef
- Category
- C++
- Title
- creare array con c++
- Category
- C++
- Title
- C++ sfinae
- Category
- C++
- Title
- find all the palindrome substring in a given string
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- hello world c++
- Category
- C++
- Title
- c++ string manipulation
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- variable sized arrays hackerrank solution in c++
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- vector in c++ class
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- const in c++
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- elseif c++
- Category
- C++
- Title
- c++ variable arguments
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- c++ map insert
- Category
- C++
- Title
- mysqli connect
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- c++ operator overloading
- Category
- C++
- Title
- how to type cast quotient of two integers to double with c++
- Category
- C++
- Title
- c++ sort array of ints
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- container class in c++
- Category
- C++
- Title
- varint index
- Category
- C++
- Title
- gta online
- Category
- C++
- Title
- ue4 c++ how to open a blueprint widget
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- how to get last element of set in c++
- Category
- C++