-
-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8d2787
commit 6152c10
Showing
1 changed file
with
6 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -6,16 +6,19 @@ This approach ensures that we find the most efficient way to make change and han | |
|
||
## Explanation | ||
|
||
1. **Initialize Coins Usage Tracker**: | ||
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**: | ||
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). | ||
Check failure on line 17 in exercises/practice/change/.approaches/dynamic-programming/content.md GitHub Actions / Lint Markdown filesTrailing spaces
|
||
- 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**: | ||
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. | ||
|
||
|