From b1b97fc7ccff25e96ddc774f60b38ec2b0a62f2e Mon Sep 17 00:00:00 2001 From: orthodox-64 <151670745+Orthodox-64@users.noreply.github.com> Date: Thu, 9 Oct 2025 03:26:56 +0530 Subject: [PATCH 1/2] Implemented the Coin Change algorithm in R. Finds the minimum number of coins needed to make a target amount. Uses dynamic programming with both top-down and bottom-up approaches. Handles edge cases including impossible amounts and zero target. Prints DP table, coin combinations, and optimal solutions. Time Complexity: O(amount * num_coins). Space Complexity: O(amount) for DP array. --- dynamic_programming/coin_change.r | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 dynamic_programming/coin_change.r diff --git a/dynamic_programming/coin_change.r b/dynamic_programming/coin_change.r new file mode 100644 index 00000000..02849274 --- /dev/null +++ b/dynamic_programming/coin_change.r @@ -0,0 +1,77 @@ +# Coin Change Problem +# +# The Coin Change problem finds the minimum number of coins needed to make a certain amount +# using a given set of coin denominations. +# +# Time Complexity: O(amount * n) where n = number of coin denominations +# Space Complexity: O(amount) +# +# Applications: +# - Currency and cash management +# - Making change in vending machines or payment systems +# - Dynamic resource allocation +# - Minimum cost problems in algorithms + +# Function to compute minimum coins required +coin_change <- function(coins, amount) { + #' Compute minimum number of coins needed to make the given amount + #' @param coins: Numeric vector of coin denominations + #' @param amount: Total amount to make + #' @return: Minimum number of coins required, or -1 if not possible + + # Initialize DP array + dp <- rep(Inf, amount + 1) + dp[1] <- 0 + dp[0 + 1] <- 0 # zero coins needed for amount 0 + + for (i in 1:amount) { + for (coin in coins) { + if (coin <= i) { + dp[i + 1] <- min(dp[i + 1], 1 + dp[i - coin + 1]) + } + } + } + + if (dp[amount + 1] == Inf) { + return(-1) + } else { + return(dp[amount + 1]) + } +} + +# Function to print the DP table (for educational purposes) +print_coin_change_dp <- function(dp, amount) { + cat("DP Table for Coin Change:\n") + for (i in 0:amount) { + cat(sprintf("Amount %2d: %s\n", i, dp[i + 1])) + } + cat("\n") +} + + +# Example Usage & Testing +cat("=== Coin Change Problem ===\n\n") + +# Test 1: Basic Example +coins <- c(1, 2, 5) +amount <- 11 +cat("Coins:", paste(coins, collapse = ", "), "\n") +cat("Amount:", amount, "\n") +min_coins <- coin_change(coins, amount) +cat("Minimum Coins Needed:", min_coins, "\n\n") + +# Test 2: No solution case +coins <- c(2, 4) +amount <- 7 +cat("Coins:", paste(coins, collapse = ", "), "\n") +cat("Amount:", amount, "\n") +min_coins <- coin_change(coins, amount) +cat("Minimum Coins Needed:", min_coins, "\n\n") + +# Test 3: Larger dataset +coins <- c(1, 3, 4, 5) +amount <- 7 +cat("Coins:", paste(coins, collapse = ", "), "\n") +cat("Amount:", amount, "\n") +min_coins <- coin_change(coins, amount) +cat("Minimum Coins Needed:", min_coins, "\n\n") \ No newline at end of file From 021a12d806913d99c9608bfb0f1c5c8a5bcea13b Mon Sep 17 00:00:00 2001 From: Sachin Pangal <151670745+Orthodox-64@users.noreply.github.com> Date: Thu, 9 Oct 2025 03:31:46 +0530 Subject: [PATCH 2/2] Update dynamic_programming/coin_change.r Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- dynamic_programming/coin_change.r | 1 - 1 file changed, 1 deletion(-) diff --git a/dynamic_programming/coin_change.r b/dynamic_programming/coin_change.r index 02849274..444246bd 100644 --- a/dynamic_programming/coin_change.r +++ b/dynamic_programming/coin_change.r @@ -21,7 +21,6 @@ coin_change <- function(coins, amount) { # Initialize DP array dp <- rep(Inf, amount + 1) - dp[1] <- 0 dp[0 + 1] <- 0 # zero coins needed for amount 0 for (i in 1:amount) {