mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-19 18:23:50 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -1,49 +1,49 @@
|
||||
# Unbounded Knapsack Problem
|
||||
# Unbounded knapsack problem
|
||||
|
||||
In this section, we first solve another common knapsack problem: the unbounded knapsack, and then explore a special case of it: the coin change problem.
|
||||
We will now solve another common knapsack variant, unbounded knapsack, and then examine a special case: coin change.
|
||||
|
||||
## Unbounded Knapsack Problem
|
||||
## Unbounded knapsack
|
||||
|
||||
!!! question
|
||||
|
||||
Given $n$ items, where the weight of the $i$-th item is $wgt[i-1]$ and its value is $val[i-1]$, and a knapsack with capacity $cap$. **Each item can be selected multiple times**. What is the maximum value that can be placed in the knapsack within the capacity limit? An example is shown in the figure below.
|
||||
Given $n$ items, where item $i$ has weight $wgt[i-1]$ and value $val[i-1]$, and a knapsack of capacity $cap$, **each item may be chosen repeatedly**. What is the maximum value that fits within the capacity? An example is shown in the figure below.
|
||||
|
||||

|
||||

|
||||
|
||||
### Dynamic Programming Approach
|
||||
### Dynamic programming approach
|
||||
|
||||
The unbounded knapsack problem is very similar to the 0-1 knapsack problem, **differing only in that there is no limit on the number of times an item can be selected**.
|
||||
Unbounded knapsack is very similar to 0-1 knapsack. **The only difference is that there is no limit on how many times an item may be chosen**.
|
||||
|
||||
- In the 0-1 knapsack problem, there is only one of each type of item, so after placing item $i$ in the knapsack, we can only choose from the first $i-1$ items.
|
||||
- In the unbounded knapsack problem, the quantity of each type of item is unlimited, so after placing item $i$ in the knapsack, **we can still choose from the first $i$ items**.
|
||||
- In 0-1 knapsack, there is only one of each item, so after including item $i$, we can choose only from the first $i-1$ items.
|
||||
- In unbounded knapsack, each item has an unlimited supply, so after including item $i$, **we can still choose from the first $i$ items**.
|
||||
|
||||
Under the rules of the unbounded knapsack problem, the changes in state $[i, c]$ are divided into two cases.
|
||||
For unbounded knapsack, state $[i, c]$ has two cases.
|
||||
|
||||
- **Not putting item $i$**: Same as the 0-1 knapsack problem, transfer to $[i-1, c]$.
|
||||
- **Putting item $i$**: Different from the 0-1 knapsack problem, transfer to $[i, c-wgt[i-1]]$.
|
||||
- **Leave item $i$ out**: As in 0-1 knapsack, the remaining state is $[i-1, c]$.
|
||||
- **Put item $i$ in**: Unlike 0-1 knapsack, the remaining state is $[i, c-wgt[i-1]]$.
|
||||
|
||||
Thus, the state transition equation becomes:
|
||||
The state transition equation becomes:
|
||||
|
||||
$$
|
||||
dp[i, c] = \max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1])
|
||||
$$
|
||||
|
||||
### Code Implementation
|
||||
### Code implementation
|
||||
|
||||
Comparing the code for the two problems, there is one change in state transition from $i-1$ to $i$, with everything else identical:
|
||||
Compared with 0-1 knapsack, only one $i-1$ in the transition changes to $i$; the rest of the code is identical:
|
||||
|
||||
```src
|
||||
[file]{unbounded_knapsack}-[class]{}-[func]{unbounded_knapsack_dp}
|
||||
```
|
||||
|
||||
### Space Optimization
|
||||
### Space optimization
|
||||
|
||||
Since the current state is transferred from states on the left and above, **after space optimization, each row in the $dp$ table should be traversed in forward order**.
|
||||
The current state depends on states to its left and above, **so after space optimization, each row of the $dp$ table must be processed in forward order**.
|
||||
|
||||
This traversal order is exactly opposite to the 0-1 knapsack. Please refer to the figure below to understand the difference between the two.
|
||||
This is the opposite of 0-1 knapsack. Use the figure below to understand the difference.
|
||||
|
||||
=== "<1>"
|
||||

|
||||

|
||||
|
||||
=== "<2>"
|
||||

|
||||
@@ -60,67 +60,67 @@ This traversal order is exactly opposite to the 0-1 knapsack. Please refer to th
|
||||
=== "<6>"
|
||||

|
||||
|
||||
The code implementation is relatively simple, just delete the first dimension of the array `dp`:
|
||||
The implementation simply removes the first dimension of `dp`:
|
||||
|
||||
```src
|
||||
[file]{unbounded_knapsack}-[class]{}-[func]{unbounded_knapsack_dp_comp}
|
||||
```
|
||||
|
||||
## Coin Change Problem
|
||||
## Coin change problem
|
||||
|
||||
The knapsack problem represents a large class of dynamic programming problems and has many variants, such as the coin change problem.
|
||||
Knapsack represents a broad family of dynamic programming problems, with many variants such as coin change.
|
||||
|
||||
!!! question
|
||||
|
||||
Given $n$ types of coins, where the denomination of the $i$-th type of coin is $coins[i - 1]$, and the target amount is $amt$. **Each type of coin can be selected multiple times**. What is the minimum number of coins needed to make up the target amount? If it is impossible to make up the target amount, return $-1$. An example is shown in the figure below.
|
||||
Given $n$ coin denominations, where denomination $i$ has value $coins[i - 1]$, and a target amount $amt$, **each denomination may be used repeatedly**. What is the minimum number of coins needed to make the target? Return $-1$ if it cannot be made. An example is shown in the figure below.
|
||||
|
||||

|
||||

|
||||
|
||||
### Dynamic Programming Approach
|
||||
### Dynamic programming approach
|
||||
|
||||
**The coin change problem can be viewed as a special case of the unbounded knapsack problem**, with the following connections and differences.
|
||||
**Coin change can be viewed as a special case of unbounded knapsack**, with the following connections and differences.
|
||||
|
||||
- The two problems can be converted to each other: "item" corresponds to "coin", "item weight" corresponds to "coin denomination", and "knapsack capacity" corresponds to "target amount".
|
||||
- The optimization goals are opposite: the unbounded knapsack problem aims to maximize item value, while the coin change problem aims to minimize the number of coins.
|
||||
- The unbounded knapsack problem seeks solutions "not exceeding" the knapsack capacity, while the coin change problem seeks solutions that "exactly" make up the target amount.
|
||||
- The two problems can be translated into one another: items correspond to coins, item weights to coin denominations, and knapsack capacity to the target amount.
|
||||
- The optimization objectives are opposite: unbounded knapsack maximizes item value, while coin change minimizes the number of coins.
|
||||
- Unbounded knapsack must stay within a capacity limit, whereas coin change must reach the target amount exactly.
|
||||
|
||||
**Step 1: Think about the decisions in each round, define the state, and thus obtain the $dp$ table**
|
||||
**Step 1: Identify each decision, define the state, and establish the $dp$ table**
|
||||
|
||||
State $[i, a]$ corresponds to the subproblem: **the minimum number of coins among the first $i$ types of coins that can make up amount $a$**, denoted as $dp[i, a]$.
|
||||
State $[i, a]$ represents the subproblem of finding **the minimum number of coins needed to make amount $a$ using the first $i$ denominations**, denoted by $dp[i, a]$.
|
||||
|
||||
The two-dimensional $dp$ table has size $(n+1) \times (amt+1)$.
|
||||
|
||||
**Step 2: Identify the optimal substructure, and then derive the state transition equation**
|
||||
**Step 2: Identify optimal substructure and derive the state transition equation**
|
||||
|
||||
This problem differs from the unbounded knapsack problem in the following two aspects regarding the state transition equation.
|
||||
The state transition differs from unbounded knapsack in two ways.
|
||||
|
||||
- This problem seeks the minimum value, so the operator $\max()$ needs to be changed to $\min()$.
|
||||
- The optimization target is the number of coins rather than item value, so when a coin is selected, simply add $1$.
|
||||
- We want a minimum, so replace $\max()$ with $\min()$.
|
||||
- We optimize the coin count rather than item value, so choosing a coin adds $+1$.
|
||||
|
||||
$$
|
||||
dp[i, a] = \min(dp[i-1, a], dp[i, a - coins[i-1]] + 1)
|
||||
$$
|
||||
|
||||
**Step 3: Determine boundary conditions and state transition order**
|
||||
**Step 3: Determine the boundary conditions and evaluation order**
|
||||
|
||||
When the target amount is $0$, the minimum number of coins needed to make it up is $0$, so all $dp[i, 0]$ in the first column equal $0$.
|
||||
A target amount of $0$ requires $0$ coins, so every entry $dp[i, 0]$ in the first column is $0$.
|
||||
|
||||
When there are no coins, **it is impossible to make up any amount $> 0$**, which is an invalid solution. To enable the $\min()$ function in the state transition equation to identify and filter out invalid solutions, we consider using $+ \infty$ to represent them, i.e., set all $dp[0, a]$ in the first row to $+ \infty$.
|
||||
With no coins, **any target amount $> 0$ is impossible**. To let $\min()$ recognize and discard these infeasible solutions, represent them by $+ \infty$: set the first-row entries $dp[0, a]$ to $+ \infty$.
|
||||
|
||||
### Code Implementation
|
||||
### Code implementation
|
||||
|
||||
Most programming languages do not provide a $+ \infty$ variable, and can only use the maximum value of integer type `int` as a substitute. However, this can lead to integer overflow: the $+ 1$ operation in the state transition equation may cause overflow.
|
||||
Most programming languages do not provide an integer value for $+ \infty$, so the maximum `int` value is used instead. However, the $+ 1$ in the transition may then overflow.
|
||||
|
||||
For this reason, we use the number $amt + 1$ to represent invalid solutions, because the maximum number of coins needed to make up $amt$ is at most $amt$. Before returning, check whether $dp[n, amt]$ equals $amt + 1$; if so, return $-1$, indicating that the target amount cannot be made up. The code is as follows:
|
||||
Instead, use $amt + 1$ to represent an infeasible solution, since making $amt$ requires at most $amt$ coins. Before returning, check whether $dp[n, amt]$ equals $amt + 1$. If so, return $-1$ to indicate that the target cannot be made. The code is as follows:
|
||||
|
||||
```src
|
||||
[file]{coin_change}-[class]{}-[func]{coin_change_dp}
|
||||
```
|
||||
|
||||
The figure below shows the dynamic programming process for coin change, which is very similar to the unbounded knapsack problem.
|
||||
The figure below shows dynamic programming for coin change, which closely resembles unbounded knapsack.
|
||||
|
||||
=== "<1>"
|
||||

|
||||

|
||||
|
||||
=== "<2>"
|
||||

|
||||
@@ -164,43 +164,43 @@ The figure below shows the dynamic programming process for coin change, which is
|
||||
=== "<15>"
|
||||

|
||||
|
||||
### Space Optimization
|
||||
### Space optimization
|
||||
|
||||
The space optimization for the coin change problem is handled in the same way as the unbounded knapsack problem:
|
||||
Space optimization is the same as for unbounded knapsack:
|
||||
|
||||
```src
|
||||
[file]{coin_change}-[class]{}-[func]{coin_change_dp_comp}
|
||||
```
|
||||
|
||||
## Coin Change Problem II
|
||||
## Coin change problem II
|
||||
|
||||
!!! question
|
||||
|
||||
Given $n$ types of coins, where the denomination of the $i$-th type of coin is $coins[i - 1]$, and the target amount is $amt$. Each type of coin can be selected multiple times. **What is the number of coin combinations that can make up the target amount?** An example is shown in the figure below.
|
||||
Given $n$ coin denominations, where denomination $i$ has value $coins[i - 1]$, and a target amount $amt$, each denomination may be used repeatedly. **How many combinations of coins make the target amount**? An example is shown in the figure below.
|
||||
|
||||

|
||||

|
||||
|
||||
### Dynamic Programming Approach
|
||||
### Dynamic programming approach
|
||||
|
||||
Compared to the previous problem, this problem's goal is to find the number of combinations, so the subproblem becomes: **the number of combinations among the first $i$ types of coins that can make up amount $a$**. The $dp$ table remains a two-dimensional matrix of size $(n+1) \times (amt + 1)$.
|
||||
This time, we count combinations, so the subproblem becomes **the number of combinations that make amount $a$ using the first $i$ denominations**. The $dp$ table remains a two-dimensional matrix of size $(n+1) \times (amt + 1)$.
|
||||
|
||||
The number of combinations for the current state equals the sum of the combinations from not selecting the current coin and selecting the current coin. The state transition equation is:
|
||||
The number of combinations for the current state is the sum of those that exclude the current coin and those that include it. The state transition equation is:
|
||||
|
||||
$$
|
||||
dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]]
|
||||
$$
|
||||
|
||||
When the target amount is $0$, no coins need to be selected to make up the target amount, so all $dp[i, 0]$ in the first column should be initialized to $1$. When there are no coins, it is impossible to make up any amount $>0$, so all $dp[0, a]$ in the first row equal $0$.
|
||||
A target of $0$ can be made by choosing no coins, so initialize every first-column entry $dp[i, 0]$ to $1$. With no coins, any target $>0$ is impossible, so the first-row entries $dp[0, a]$ are $0$.
|
||||
|
||||
### Code Implementation
|
||||
### Code implementation
|
||||
|
||||
```src
|
||||
[file]{coin_change_ii}-[class]{}-[func]{coin_change_ii_dp}
|
||||
```
|
||||
|
||||
### Space Optimization
|
||||
### Space optimization
|
||||
|
||||
The space optimization is handled in the same way, just delete the coin dimension:
|
||||
The space optimization is the same: remove the coin dimension:
|
||||
|
||||
```src
|
||||
[file]{coin_change_ii}-[class]{}-[func]{coin_change_ii_dp_comp}
|
||||
|
||||
Reference in New Issue
Block a user