Skip to content

Commit

Permalink
Updating approach files
Browse files Browse the repository at this point in the history
  • Loading branch information
jagdish-15 committed Nov 15, 2024
1 parent e338ff2 commit 9438325
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 45 deletions.
2 changes: 1 addition & 1 deletion exercises/practice/change/.approaches/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"jagdish-15"
],
"contributors": [
"kagoh"
"kahgoh"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
# Dynamic Programming Approach

The **Dynamic Programming (DP)** approach is an efficient way to solve the problem of making change for a given total using a list of available coin denominations. It minimizes the number of coins needed by breaking down the problem into smaller subproblems and solving them progressively.

This approach ensures that we find the most efficient way to make change and handles edge cases where no solution exists.

## Explanation

1. **Initialize Coins Usage Tracker**:

- We create a list `coinsUsed`, where each index `i` stores the most efficient combination of coins that sum up to the value `i`.
- The list is initialized with an empty list at index `0`, as no coins are needed to achieve a total of zero.

2. **Iterative Dynamic Programming**:

- For each value `i` from 1 to `grandTotal`, we explore all available coin denominations to find the best combination that can achieve the total `i`.
- For each coin, we check if it can be part of the solution (i.e., if `coin <= i` and `coinsUsed[i - coin]` is a valid combination).
- If so, we generate a new combination by adding the current coin to the solution for `i - coin`. We then compare the size of this new combination with the existing best combination and keep the one with fewer coins.

3. **Result**:

- After processing all values up to `grandTotal`, the combination at `coinsUsed[grandTotal]` will represent the most efficient solution.
- If no valid combination exists for `grandTotal`, an exception is thrown.

```java
import java.util.List;
import java.util.ArrayList;
Expand Down Expand Up @@ -61,6 +39,29 @@ class ChangeCalculator {
}
```

The **Dynamic Programming (DP)** approach is an efficient way to solve the problem of making change for a given total using a list of available coin denominations.
It minimizes the number of coins needed by breaking down the problem into smaller subproblems and solving them progressively.

This approach ensures that we find the most efficient way to make change and handles edge cases where no solution exists.

## Explanation

1. **Initialize Coins Usage Tracker**:

- We create a list `coinsUsed`, where each index `i` stores the most efficient combination of coins that sum up to the value `i`.
- The list is initialized with an empty list at index `0`, as no coins are needed to achieve a total of zero.

2. **Iterative Dynamic Programming**:

- For each value `i` from 1 to `grandTotal`, we explore all available coin denominations to find the best combination that can achieve the total `i`.
- For each coin, we check if it can be part of the solution (i.e., if `coin <= i` and `coinsUsed[i - coin]` is a valid combination).
- If so, we generate a new combination by adding the current coin to the solution for `i - coin`. We then compare the size of this new combination with the existing best combination and keep the one with fewer coins.

3. **Result**:

- After processing all values up to `grandTotal`, the combination at `coinsUsed[grandTotal]` will represent the most efficient solution.
- If no valid combination exists for `grandTotal`, an exception is thrown.

## Key Points

- **Time Complexity**: The time complexity of this approach is **O(n * m)**, where `n` is the `grandTotal` and `m` is the number of available coin denominations. This is because we iterate over all coin denominations for each amount up to `grandTotal`.
Expand All @@ -72,15 +73,7 @@ class ChangeCalculator {
- If the `grandTotal` is negative, an exception is thrown immediately.
- If there is no way to make the exact total with the given denominations, an exception is thrown with a descriptive message.

## Trade-offs and Considerations

- **Efficiency**: This approach is highly efficient in terms of minimizing the number of coins, but it might require significant memory for larger `grandTotal` values, as the space complexity grows linearly with `grandTotal`.

- **Alternative Approaches**:

- A **Greedy Approach** could be faster for some cases but does not always guarantee the minimum number of coins.
- This dynamic programming approach is best when the goal is to guarantee the fewest coins possible, especially when no simple greedy solution exists.

## Conclusion

The dynamic programming approach provides an optimal solution for the change-making problem, ensuring that we minimize the number of coins used while efficiently solving the problem for any `grandTotal`. However, it’s essential to consider the trade-offs in terms of memory usage and the time complexity when dealing with very large inputs.
The dynamic programming approach provides an optimal solution for the change-making problem, ensuring that we minimize the number of coins used while efficiently solving the problem for any `grandTotal`.
However, it’s essential to consider the trade-offs in terms of memory usage and the time complexity when dealing with very large inputs.
25 changes: 13 additions & 12 deletions exercises/practice/change/.approaches/introduction.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# Introduction to Change Calculator
# Introduction

In the "Change Calculator" exercise, the goal is to determine the minimum number of coins needed to reach a given total using a specific set of coin denominations. This is a classic problem in dynamic programming, where efficient change-making is essential, especially when there are constraints on coin types or large totals.
There is an idiomatic approach to solving "Change."
You can use [dynamic programming][dynamic-programming] to calculate the minimum number of coins required for a given total.

## Problem Overview
## General guidance

Given:
The key to solving "Change" is understanding that not all totals can be reached with the available coin denominations.
The solution needs to figure out which totals can be achieved and how to combine the coins optimally.

- A list of coin denominations, each representing an available currency unit.
- A total amount (`grandTotal`) we want to reach using the fewest possible coins from the given denominations.
## Approach: Dynamic Programming

The solution should find the optimal combination of coins to match the total. If it's impossible to match the total exactly, the solution should indicate this by throwing an exception.

## Approach Overview

Our solution uses a **dynamic programming approach**, where we systematically build up the optimal combinations for all totals from `0` up to the target amount (`grandTotal`). For each total, we track the fewest coins needed to make that total, reusing previous results to make the solution efficient.
Our solution uses a **dynamic programming approach**, where we systematically build up the optimal combinations for all totals from `0` up to the target amount (`grandTotal`).
For each total, we track the fewest coins needed to make that total, reusing previous results to make the solution efficient.

This approach ensures that we find the minimum number of coins required in a structured, repeatable way, avoiding the need for complex recursive calls or excessive backtracking.

Expand All @@ -23,4 +21,7 @@ This approach ensures that we find the minimum number of coins required in a str
- **Flexibility**: Handles cases where exact change is impossible by checking at each step.
- **Scalability**: Works for various coin denominations and totals, though large inputs may impact performance.

For a detailed look at the code and logic, see the full explanation in the [approach file](https://exercism.org/tracks/java/exercises/change/approaches/dynamic-programming).
For a detailed look at the code and logic, see the full explanation in the [Dynamic Programming Approach][approach-dynamic-programming].

[approach-dynamic-programming]: https://exercism.org/tracks/java/exercises/change/approaches/dynamic-programming
[dynamic-programming]: https://en.wikipedia.org/wiki/Dynamic_programming

Check failure on line 27 in exercises/practice/change/.approaches/introduction.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Files should end with a single newline character

exercises/practice/change/.approaches/introduction.md:27:72 MD047/single-trailing-newline Files should end with a single newline character https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md047.md

0 comments on commit 9438325

Please sign in to comment.