Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding approach change #2859

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e3cbf38
Adding approach change
jagdish-15 Nov 9, 2024
4a7811f
Fixing typo in filename
jagdish-15 Nov 9, 2024
e8d2787
Fixing style errors
jagdish-15 Nov 9, 2024
6152c10
Fixing style errors
jagdish-15 Nov 9, 2024
5d398d0
Fixing stle errors
jagdish-15 Nov 9, 2024
115a331
Fixing file names
jagdish-15 Nov 9, 2024
d2c2451
Adding uuid for approach of change
jagdish-15 Nov 14, 2024
b3a98df
Adding snippet for approach of change
jagdish-15 Nov 14, 2024
e338ff2
Merge branch 'main' into add-approach-change
jagdish-15 Nov 14, 2024
9438325
Updating approach files
jagdish-15 Nov 15, 2024
fde837b
Fixing styling errors
jagdish-15 Nov 15, 2024
7ec2960
Adding approach change
jagdish-15 Nov 9, 2024
404de05
Fixing typo in filename
jagdish-15 Nov 9, 2024
1aa6d9c
Fixing style errors
jagdish-15 Nov 9, 2024
2fbec2a
Fixing style errors
jagdish-15 Nov 9, 2024
c1d07e5
Fixing stle errors
jagdish-15 Nov 9, 2024
afee45a
Fixing file names
jagdish-15 Nov 9, 2024
d9694fa
Adding uuid for approach of change
jagdish-15 Nov 14, 2024
7aa412e
Adding snippet for approach of change
jagdish-15 Nov 14, 2024
f3eb007
Updating approach files
jagdish-15 Nov 15, 2024
0b5b6f2
Fixing styling errors
jagdish-15 Nov 15, 2024
2df72c4
Merge branch 'add-approach-change' of https://github.com/jagdish-15/j…
jagdish-15 Nov 18, 2024
2758bc3
Updating approaches for Change after feedback
jagdish-15 Nov 18, 2024
c0a46c6
Fixing styling issues for approache for Change
jagdish-15 Nov 18, 2024
f76ad5d
Update exercises/practice/change/.approaches/dynamic-programming/cont…
jagdish-15 Nov 19, 2024
5db8204
Update exercises/practice/change/.approaches/dynamic-programming/cont…
jagdish-15 Nov 19, 2024
505a92d
Updating content for dynamic approach of change
jagdish-15 Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions exercises/practice/change/.approaches/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"introduction": {
"authors": [
"jagdish-15"
]
},
"approaches": [
{
"uuid": "d0b615ca-3a02-4d66-ad10-e0c513062189",
"slug": "dynamic-programming",
"title": "Dynamic Programming Approach",
"blurb": "Use dynamic programming to find the most efficient change combination.",
"authors": [
"jagdish-15"
],
"contributors": [
"kahgoh"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Dynamic Programming Approach

```java
import java.util.List;
import java.util.ArrayList;

class ChangeCalculator {
private final List<Integer> currencyCoins;

ChangeCalculator(List<Integer> currencyCoins) {
this.currencyCoins = currencyCoins;
}

List<Integer> computeMostEfficientChange(int grandTotal) {
if (grandTotal < 0)
throw new IllegalArgumentException("Negative totals are not allowed.");

List<List<Integer>> coinsUsed = new ArrayList<>(grandTotal + 1);
coinsUsed.add(new ArrayList<Integer>());

for (int i = 1; i <= grandTotal; i++) {
List<Integer> bestCombination = null;
for (int coin: currencyCoins) {
if (coin <= i && coinsUsed.get(i - coin) != null) {
List<Integer> currentCombination = new ArrayList<>(coinsUsed.get(i - coin));
currentCombination.add(0, coin);
if (bestCombination == null || currentCombination.size() < bestCombination.size())
bestCombination = currentCombination;
}
}
coinsUsed.add(bestCombination);
}

if (coinsUsed.get(grandTotal) == null)
throw new IllegalArgumentException("The total " + grandTotal + " cannot be represented in the given currency.");

return coinsUsed.get(grandTotal);
}
}
```

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.

## Explanation
kahgoh marked this conversation as resolved.
Show resolved Hide resolved

1. **Initialize Coins Usage Tracker**:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to one the comments on the Queen Attack approach, should this be a heading?

Suggested change
1. **Initialize Coins Usage Tracker**:
### Initialize Coins Usage Tracker


- If the `grandTotal` is negative, an exception is thrown immediately.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the making Initialize Coins Usage Tracker a heading the indent should be removed.

Suggested change
- If the `grandTotal` is negative, an exception is thrown immediately.
- If the `grandTotal` is negative, an exception is thrown immediately.

- 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**:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2. **Iterative Dynamic Programming**:
### 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**:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
3. **Result**:
### 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.

## Time and Space 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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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`.
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`.


- The space complexity is **O(n)** due to the list `coinsUsed`, which stores the most efficient coin combination for each total up to `grandTotal`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- The space complexity is **O(n)** due to the list `coinsUsed`, which stores the most efficient coin combination for each total up to `grandTotal`.
The space complexity is **O(n)** due to the list `coinsUsed`, which stores the most efficient coin combination for each total up to `grandTotal`.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ChangeCalculator {
private final List<Integer> currencyCoins;

ChangeCalculator(List<Integer> currencyCoins) {
this.currencyCoins = currencyCoins;
}
// computeMostEfficientChange method
}
55 changes: 55 additions & 0 deletions exercises/practice/change/.approaches/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Introduction

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.

## General guidance

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.

## Approach: Dynamic Programming

jagdish-15 marked this conversation as resolved.
Show resolved Hide resolved
```java
import java.util.List;
import java.util.ArrayList;

class ChangeCalculator {
private final List<Integer> currencyCoins;

ChangeCalculator(List<Integer> currencyCoins) {
this.currencyCoins = currencyCoins;
}

List<Integer> computeMostEfficientChange(int grandTotal) {
if (grandTotal < 0)
throw new IllegalArgumentException("Negative totals are not allowed.");

List<List<Integer>> coinsUsed = new ArrayList<>(grandTotal + 1);
coinsUsed.add(new ArrayList<Integer>());

for (int i = 1; i <= grandTotal; i++) {
List<Integer> bestCombination = null;
for (int coin: currencyCoins) {
if (coin <= i && coinsUsed.get(i - coin) != null) {
List<Integer> currentCombination = new ArrayList<>(coinsUsed.get(i - coin));
currentCombination.add(0, coin);
if (bestCombination == null || currentCombination.size() < bestCombination.size())
bestCombination = currentCombination;
}
}
coinsUsed.add(bestCombination);
}

if (coinsUsed.get(grandTotal) == null)
throw new IllegalArgumentException("The total " + grandTotal + " cannot be represented in the given currency.");

return coinsUsed.get(grandTotal);
}
}
```

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