-
-
Notifications
You must be signed in to change notification settings - Fork 350
Implemented the Coin Change algorithm in R. #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
siriak
merged 3 commits into
TheAlgorithms:master
from
Sachinn-64:feat-coin-change-ultra-clean
Oct 11, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # 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[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])) | ||
|
siriak marked this conversation as resolved.
|
||
| } | ||
|
siriak marked this conversation as resolved.
|
||
| 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") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.