You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/practice/change/.approaches/dynamic-programming/content.md
+6-3Lines changed: 6 additions & 3 deletions
Display the source diff
Display the rich diff
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
6
6
7
7
## Explanation
8
8
9
-
1.**Initialize Coins Usage Tracker**:
9
+
1.**Initialize Coins Usage Tracker**:
10
+
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`.
11
12
- The list is initialized with an empty list at index `0`, as no coins are needed to achieve a total of zero.
12
13
13
-
2.**Iterative Dynamic Programming**:
14
+
2.**Iterative Dynamic Programming**:
15
+
14
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`.
15
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).
16
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.
17
19
18
-
3.**Result**:
20
+
3.**Result**:
21
+
19
22
- After processing all values up to `grandTotal`, the combination at `coinsUsed[grandTotal]` will represent the most efficient solution.
20
23
- If no valid combination exists for `grandTotal`, an exception is thrown.
0 commit comments