Skip to content
Open
Changes from all commits
Commits
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
28 changes: 27 additions & 1 deletion docs/Chap16/Problems/16-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,33 @@
>
> **d.** Give an $O(nk)$-time algorithm that makes change for any set of $k$ different coin denominations, assuming that one of the coins is a penny.

**a.** Always give the highest denomination coin that you can without going over. Then, repeat this process until the amount of remaining change drops to $0$.
**a.** **Algorithm:** At each step, select the largest coin denomination that does not exceed the remaining amount of change, take as many as possible (i.e., $\lfloor n / C \rfloor$ coins, where $C$ is the denomination), and recursively solve for the remaining amount.

**Proof of Correctness:** Let the coin denominations be $25$ (quarters), $10$ (dimes), $5$ (nickels), and $1$ (pennies). Suppose that in an optimal solution, the numbers of coins used for these denominations are $a, b, c, d$ respectively. First, we prove the upper bounds on the number of coins of each denomination in an optimal solution:
* $d \le 4$: Otherwise, $5$ pennies ($5$ cents) can be replaced by $1$ nickel ($5$ cents), yielding a solution with $4$ fewer coins.
* $c \le 1$: Otherwise, $2$ nickels ($10$ cents) can be replaced by $1$ dime ($10$ cents), yielding $1$ fewer coin.
* $b \le 2$: Otherwise, $3$ dimes ($30$ cents) can be replaced by $1$ quarter and $1$ nickel ($30$ cents), yielding $1$ fewer coin.
* $b = 2$ and $c = 1$ cannot hold simultaneously: Otherwise, $2$ dimes and $1$ nickel ($25$ cents) can be replaced by $1$ quarter ($25$ cents), yielding $2$ fewer coins.

From these bounds, we can establish upper bounds on the total value that can be made by combinations of lower denominations in an optimal solution:
* The maximum value made by dimes, nickels, and pennies is:
$$10b + 5c + d \le 2 \times 10 + 4 = 24 < 25$$
(since $b=2$ and $c=1$ cannot both hold, the max value is either $2 \times 10 + 0 \times 5 + 4 = 24$ or $1 \times 10 + 1 \times 5 + 4 = 19$).
Thus, dimes, nickels, and pennies can make up at most $24$ cents in an optimal solution.
* The maximum value made by nickels and pennies is:
$$5c + d \le 1 \times 5 + 4 = 9 < 10$$
Thus, nickels and pennies can make up at most $9$ cents in an optimal solution.
* The maximum value made by pennies is:
$$d \le 4 < 5$$
Thus, pennies can make up at most $4$ cents in an optimal solution.

Now we show that the greedy choice is always optimal by induction or case analysis on $n$:
* When $n \ge 25$, if the optimal solution uses $a < \lfloor n/25 \rfloor$ quarters, the remaining amount of change to be made by dimes, nickels, and pennies must be $n - 25a \ge 25$. However, dimes, nickels, and pennies can make up at most $24$ cents in an optimal solution, which is a contradiction. Thus, we must have $a = \lfloor n/25 \rfloor$ quarters, which is exactly the greedy choice.
* When $10 \le n < 25$, if the optimal solution uses $b < \lfloor n/10 \rfloor$ dimes, the remaining amount of change to be made by nickels and pennies must be $n - 10b \ge 10$. However, nickels and pennies can make up at most $9$ cents in an optimal solution, which is a contradiction. Thus, $b = \lfloor n/10 \rfloor$ dimes, matching the greedy choice.
* When $5 \le n < 10$, if the optimal solution uses $c < \lfloor n/5 \rfloor = 1$ nickels (i.e., $c = 0$), the remaining amount of change to be made by pennies must be $n - 5c = n \ge 5$. However, pennies can make up at most $4$ cents in an optimal solution, which is a contradiction. Thus, $c = \lfloor n/5 \rfloor = 1$, matching the greedy choice.
* When $n < 5$, only pennies can be used, which also matches the greedy choice.

By induction, the greedy strategy of choosing the largest denomination at each step yields an optimal solution.

**b.** Given an optimal solution $(x_0, x_1, \dots, x_k)$ where $x_i$ indicates the number of coins of denomination $c_i$ . We will first show that we must have $x_i < c$ for every $i < k$. Suppose that we had some $x_i \ge c$, then, we could decrease $x_i$ by $c$ and increase $x_{i + 1}$ by $1$. This collection of coins has the same value and has $c − 1$ fewer coins, so the original solution must of been non-optimal. This configuration of coins is exactly the same as you would get if you kept greedily picking the largest coin possible. This is because to get a total value of $V$, you would pick $x_k = \lfloor V c^{−k} \rfloor$ and for $i < k$, $x_i\lfloor (V\mod c^{i + 1})c^{-i} \rfloor$. This is the only solution that satisfies the property that there aren't more than $c$ of any but the largest denomination because the coin amounts are a base $c$ representation of $V\mod c^k$.

Expand Down