From 39aaf7dc7fc275c8de55bcdb4943826a51cf1c1b Mon Sep 17 00:00:00 2001 From: CutePigdaddy Date: Wed, 10 Jun 2026 14:53:40 +0800 Subject: [PATCH 1/2] Update 16.1.md --- docs/Chap16/Problems/16-1.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/Chap16/Problems/16-1.md b/docs/Chap16/Problems/16-1.md index 4e53f3c47d..ab420dd5a4 100755 --- a/docs/Chap16/Problems/16-1.md +++ b/docs/Chap16/Problems/16-1.md @@ -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$. From 1be07b3f1f08c3aa1732e2be79c2f95ab02269c8 Mon Sep 17 00:00:00 2001 From: Peng-Yu Chen Date: Mon, 19 Oct 2026 22:22:47 -0400 Subject: [PATCH 2/2] Update 16-1.md --- docs/Chap16/Problems/16-1.md | 43 ++++++++++++++---------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/docs/Chap16/Problems/16-1.md b/docs/Chap16/Problems/16-1.md index ab420dd5a4..bb80cf57f4 100755 --- a/docs/Chap16/Problems/16-1.md +++ b/docs/Chap16/Problems/16-1.md @@ -8,33 +8,22 @@ > > **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.** **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. +**a.** Always give the highest denomination coin that you can without going over, that is, $\lfloor n / C \rfloor$ coins of the largest denomination $C \le n$. Then, repeat this process on the remaining $n \bmod C$ cents until it drops to $0$. + +Let $a, b, c, d$ be the numbers of quarters, dimes, nickels, and pennies in an optimal solution. Each of the following would contradict optimality: + +- $d \ge 5$: $5$ pennies can be replaced by $1$ nickel, saving $4$ coins. +- $c \ge 2$: $2$ nickels can be replaced by $1$ dime, saving $1$ coin. +- $b \ge 3$: $3$ dimes can be replaced by $1$ quarter and $1$ nickel, saving $1$ coin. +- $b = 2$ and $c = 1$: $2$ dimes and $1$ nickel can be replaced by $1$ quarter, saving $2$ coins. + +Hence, in an optimal solution, + +- pennies make up $d \le 4 < 5$ cents, +- nickels and pennies make up $5c + d \le 5 + 4 = 9 < 10$ cents, and +- dimes, nickels, and pennies make up $10b + 5c + d \le \max(2 \cdot 10 + 4, 10 + 5 + 4) = 24 < 25$ cents. + +Since dimes, nickels, and pennies make up less than $25$ cents, $n - 25a < 25$, so $a = \lfloor n / 25 \rfloor$, which is the greedy choice. Let $n_1 = n - 25a$. Since nickels and pennies make up less than $10$ cents, $b = \lfloor n_1 / 10 \rfloor$. Let $n_2 = n_1 - 10b$. Since pennies make up less than $5$ cents, $c = \lfloor n_2 / 5 \rfloor$, and finally $d = n_2 - 5c$. Every count in the optimal solution is exactly the one the greedy algorithm picks, so the greedy solution is optimal. **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$.