From 7b92e8d6252da483cd3669fbaace5bb4f4cc9173 Mon Sep 17 00:00:00 2001 From: webdesignbydivyansh <62607594+webdesignbydivyansh@users.noreply.github.com> Date: Sat, 31 Oct 2020 11:42:24 +0530 Subject: [PATCH] feat: Coin Change Problem (#1378) * palindrome no a program to check if a number is palindrome or not * updated palindrome.cpp * updated palindrome.cpp missing equal to operator at line no 16 * flatten_a_binary_seach_tree This program flattens a binary search tree or in simple words, converts it into linked list in sorted form * a prog to convert bst into linked list A code that is used to convert a binary search tree into linked list * created merge k sorted arrays this program displays all the elements from the different arrays(sorted) into 1 single array(sorted). * updated braces in some parts * included braces in line no 58 * included braces in line 58 and 60 * created trap_rainwater.cpp A program to calculate the maximum amount of water that can be stored between buildings. * deleted rainwater harvesting * deleted palindrome * deleted merge k sorted arrays * deleted binary search tree to linked list * deleted binary search tree to linked list * created min_coins_topdown.cpp This is another version of coins exchange problem solved using top down approach * updated the code please take a look at it * final changes * updated my code * updating DIRECTORY.md * changed int64_t to int8_t and int16_t * edited line 3 * changed line 3 * edited line 3 & removed line 18 * added extra space Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 1 + dynamic_programming/coin_change_topdown.cpp | 89 +++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 dynamic_programming/coin_change_topdown.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index 210f30b4b..3930e67fd 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -55,6 +55,7 @@ * [Bellman Ford](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/bellman_ford.cpp) * [Catalan Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/catalan_numbers.cpp) * [Coin Change](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/coin_change.cpp) + * [Coin Change Topdown](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/coin_change_topdown.cpp) * [Cut Rod](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/cut_rod.cpp) * [Edit Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/edit_distance.cpp) * [Egg Dropping Puzzle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/egg_dropping_puzzle.cpp) diff --git a/dynamic_programming/coin_change_topdown.cpp b/dynamic_programming/coin_change_topdown.cpp new file mode 100644 index 000000000..e8112b089 --- /dev/null +++ b/dynamic_programming/coin_change_topdown.cpp @@ -0,0 +1,89 @@ +/** + * @file + * @brief [Minimum coins](https://leetcode.com/problems/coin-change/) change problem is a problem used to find the minimum number of + * coins required to completely reach a target amount. + * + * @details + * This problem can be solved using 2 methods: + * 1. Top down approach + * 2. Bottom up appraoch + * Top down approach involves a vector with all elements initialised to 0. + * It is based on optimal substructure and overlapping subproblems. + * Overall time complexity of coin change problem is O(n*t) + * For example: example 1:- + * Coins: {1,7,10} + * Target:15 + * Therfore minimum number of coins required = 3 of denomination 1,7 and 7. + * @author [Divyansh Kushwaha](https://github.com/webdesignbydivyansh) + */ + +#include // for io operations +#include // for std::vector +#include // for assert +#include // for INT_MAX + +/** + * @namespace dynamic_programming + * @brief Dynamic Programming algorithm + */ +namespace dynamic_programming { +/** + * @namespace mincoins_topdown + * @brief Functions for [minimum coin exchange](https://leetcode.com/problems/coin-change/) problem + */ +namespace mincoins_topdown { + /** + * @brief This implementation is for finding minimum number of coins . + * @param T template-type to use any kind of value + * @param n amount to be reached + * @param coins vector of coins + * @param t deontes the number of coins + * @param dp initilised to 0 + * @returns minimum number of coins + */ + template + int64_t mincoins(const T &n, const std::vector &coins, const int16_t &t, std::vector dp){ + if(n==0){ + return 0; + } + if(dp[n]!=0){ + return dp[n]; + } + int ans=INT_MAX; //variable to store min coins + for(int i=0;i=0){ //if after subtracting the current denomination is it greater than 0 or not + int sub=mincoins(n-coins[i],coins,t,dp); + ans=std::min(ans,sub+1); + } + } + dp[n]=ans; + return dp[n]; //returns minimum number of coins + } + +} // namespace mincoins_topdown +} // namespace dynamic_programming + +/** + * @brief Test implementations + * @returns void + */ +static void test() { + // example 1: number of coins=3 and minimum coins required=3(7,7,1) + const int64_t n1 = 15; + const int8_t t1=3, a1=0; + std::cout << "\nTest 1..."; + std::vector arr1 {1,7,10}; + std::vector dp1 (n1+1); + fill(dp1.begin(),dp1.end(),a1); + assert(dynamic_programming::mincoins_topdown::mincoins(n1, arr1, t1, dp1)==3); + std::cout << "Passed\n"; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // execute the test + return 0; +}