Skip to content

Commit 9438325

Browse files
committed
Updating approach files
1 parent e338ff2 commit 9438325

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
lines changed

exercises/practice/change/.approaches/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"jagdish-15"
1515
],
1616
"contributors": [
17-
"kagoh"
17+
"kahgoh"
1818
]
1919
}
2020
]

exercises/practice/change/.approaches/dynamic-programming/content.md

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
# Dynamic Programming Approach
22

3-
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.
4-
5-
This approach ensures that we find the most efficient way to make change and handles edge cases where no solution exists.
6-
7-
## Explanation
8-
9-
1. **Initialize Coins Usage Tracker**:
10-
11-
- We create a list `coinsUsed`, where each index `i` stores the most efficient combination of coins that sum up to the value `i`.
12-
- The list is initialized with an empty list at index `0`, as no coins are needed to achieve a total of zero.
13-
14-
2. **Iterative Dynamic Programming**:
15-
16-
- 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`.
17-
- 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).
18-
- 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.
19-
20-
3. **Result**:
21-
22-
- After processing all values up to `grandTotal`, the combination at `coinsUsed[grandTotal]` will represent the most efficient solution.
23-
- If no valid combination exists for `grandTotal`, an exception is thrown.
24-
253
```java
264
import java.util.List;
275
import java.util.ArrayList;
@@ -61,6 +39,29 @@ class ChangeCalculator {
6139
}
6240
```
6341

42+
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.
43+
It minimizes the number of coins needed by breaking down the problem into smaller subproblems and solving them progressively.
44+
45+
This approach ensures that we find the most efficient way to make change and handles edge cases where no solution exists.
46+
47+
## Explanation
48+
49+
1. **Initialize Coins Usage Tracker**:
50+
51+
- We create a list `coinsUsed`, where each index `i` stores the most efficient combination of coins that sum up to the value `i`.
52+
- The list is initialized with an empty list at index `0`, as no coins are needed to achieve a total of zero.
53+
54+
2. **Iterative Dynamic Programming**:
55+
56+
- 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`.
57+
- 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).
58+
- 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.
59+
60+
3. **Result**:
61+
62+
- After processing all values up to `grandTotal`, the combination at `coinsUsed[grandTotal]` will represent the most efficient solution.
63+
- If no valid combination exists for `grandTotal`, an exception is thrown.
64+
6465
## Key Points
6566

6667
- **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`.
@@ -72,15 +73,7 @@ class ChangeCalculator {
7273
- If the `grandTotal` is negative, an exception is thrown immediately.
7374
- If there is no way to make the exact total with the given denominations, an exception is thrown with a descriptive message.
7475

75-
## Trade-offs and Considerations
76-
77-
- **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`.
78-
79-
- **Alternative Approaches**:
80-
81-
- A **Greedy Approach** could be faster for some cases but does not always guarantee the minimum number of coins.
82-
- This dynamic programming approach is best when the goal is to guarantee the fewest coins possible, especially when no simple greedy solution exists.
83-
8476
## Conclusion
8577

86-
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.
78+
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`.
79+
However, it’s essential to consider the trade-offs in terms of memory usage and the time complexity when dealing with very large inputs.
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
# Introduction to Change Calculator
1+
# Introduction
22

3-
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.
3+
There is an idiomatic approach to solving "Change."
4+
You can use [dynamic programming][dynamic-programming] to calculate the minimum number of coins required for a given total.
45

5-
## Problem Overview
6+
## General guidance
67

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

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

12-
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.
13-
14-
## Approach Overview
15-
16-
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.
13+
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`).
14+
For each total, we track the fewest coins needed to make that total, reusing previous results to make the solution efficient.
1715

1816
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.
1917

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

26-
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).
24+
For a detailed look at the code and logic, see the full explanation in the [Dynamic Programming Approach][approach-dynamic-programming].
25+
26+
[approach-dynamic-programming]: https://exercism.org/tracks/java/exercises/change/approaches/dynamic-programming
27+
[dynamic-programming]: https://en.wikipedia.org/wiki/Dynamic_programming

0 commit comments

Comments
 (0)