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
+1-13Lines changed: 1 addition & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,12 +42,11 @@ class ChangeCalculator {
42
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
43
It minimizes the number of coins needed by breaking down the problem into smaller subproblems and solving them progressively.
44
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
45
## Explanation
48
46
49
47
1.**Initialize Coins Usage Tracker**:
50
48
49
+
- If the `grandTotal` is negative, an exception is thrown immediately.
51
50
- We create a list `coinsUsed`, where each index `i` stores the most efficient combination of coins that sum up to the value `i`.
52
51
- The list is initialized with an empty list at index `0`, as no coins are needed to achieve a total of zero.
53
52
@@ -62,18 +61,7 @@ This approach ensures that we find the most efficient way to make change and han
62
61
- After processing all values up to `grandTotal`, the combination at `coinsUsed[grandTotal]` will represent the most efficient solution.
63
62
- If no valid combination exists for `grandTotal`, an exception is thrown.
64
63
65
-
## Key Points
66
64
67
65
-**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`.
68
66
69
67
-**Space Complexity**: The space complexity is **O(n)** due to the list `coinsUsed`, which stores the most efficient coin combination for each total up to `grandTotal`.
70
-
71
-
-**Edge Cases**:
72
-
73
-
- If the `grandTotal` is negative, an exception is thrown immediately.
74
-
- If there is no way to make the exact total with the given denominations, an exception is thrown with a descriptive message.
75
-
76
-
## Conclusion
77
-
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.
Copy file name to clipboardExpand all lines: exercises/practice/change/.approaches/introduction.md
+38-10Lines changed: 38 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,16 +10,44 @@ The solution needs to figure out which totals can be achieved and how to combine
10
10
11
11
## Approach: Dynamic Programming
12
12
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.
15
-
16
-
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.
17
-
18
-
## Key Features of the Approach
19
-
20
-
-**Efficiency**: By building solutions for each increment up to `grandTotal`, this approach minimizes redundant calculations.
21
-
-**Flexibility**: Handles cases where exact change is impossible by checking at each step.
22
-
-**Scalability**: Works for various coin denominations and totals, though large inputs may impact performance.
0 commit comments