mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-11 03:10:58 +00:00
build
This commit is contained in:
@@ -6,7 +6,7 @@ comments: true
|
||||
|
||||
!!! question
|
||||
|
||||
Given $n$ items, the weight of the $i$-th item is $wgt[i-1]$ and its value is $val[i-1]$, and a knapsack with a capacity of $cap$. Each item can be chosen only once, **but a part of the item can be selected, with its value calculated based on the proportion of the weight chosen**, what is the maximum value of the items in the knapsack under the limited capacity? An example is shown below.
|
||||
Given $n$ items, the weight of the $i$-th item is $wgt[i-1]$ and its value is $val[i-1]$, and a knapsack with a capacity of $cap$. Each item can be chosen only once, **but a part of the item can be selected, with its value calculated based on the proportion of the weight chosen**, what is the maximum value of the items in the knapsack under the limited capacity? An example is shown in Figure 15-3.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -14,7 +14,7 @@ comments: true
|
||||
|
||||
The fractional knapsack problem is very similar overall to the 0-1 knapsack problem, involving the current item $i$ and capacity $c$, aiming to maximize the value within the limited capacity of the knapsack.
|
||||
|
||||
The difference is that, in this problem, only a part of an item can be chosen. As shown in the Figure 15-4 , **we can arbitrarily split the items and calculate the corresponding value based on the weight proportion**.
|
||||
The difference is that, in this problem, only a part of an item can be chosen. As shown in Figure 15-4, **we can arbitrarily split the items and calculate the corresponding value based on the weight proportion**.
|
||||
|
||||
1. For item $i$, its value per unit weight is $val[i-1] / wgt[i-1]$, referred to as the unit value.
|
||||
2. Suppose we put a part of item $i$ with weight $w$ into the knapsack, then the value added to the knapsack is $w \times val[i-1] / wgt[i-1]$.
|
||||
@@ -25,7 +25,7 @@ The difference is that, in this problem, only a part of an item can be chosen. A
|
||||
|
||||
### 1. Greedy strategy determination
|
||||
|
||||
Maximizing the total value of the items in the knapsack essentially means maximizing the value per unit weight. From this, the greedy strategy shown below can be deduced.
|
||||
Maximizing the total value of the items in the knapsack essentially means maximizing the value per unit weight. From this, the greedy strategy shown in Figure 15-5 can be deduced.
|
||||
|
||||
1. Sort the items by their unit value from high to low.
|
||||
2. Iterate over all items, **greedily choosing the item with the highest unit value in each round**.
|
||||
@@ -526,7 +526,7 @@ Now remove a unit weight of any item from the knapsack and replace it with a uni
|
||||
|
||||
For other items in this solution, we can also construct the above contradiction. Overall, **items with greater unit value are always better choices**, proving that the greedy strategy is effective.
|
||||
|
||||
As shown in the Figure 15-6 , if the item weight and unit value are viewed as the horizontal and vertical axes of a two-dimensional chart respectively, the fractional knapsack problem can be transformed into "seeking the largest area enclosed within a limited horizontal axis range". This analogy can help us understand the effectiveness of the greedy strategy from a geometric perspective.
|
||||
As shown in Figure 15-6, if the item weight and unit value are viewed as the horizontal and vertical axes of a two-dimensional chart respectively, the fractional knapsack problem can be transformed into "seeking the largest area enclosed within a limited horizontal axis range". This analogy can help us understand the effectiveness of the greedy strategy from a geometric perspective.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ Let's first understand the working principle of the greedy algorithm through the
|
||||
|
||||
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$, with each type of coin available indefinitely, what is the minimum number of coins needed to make up the target amount? If it is not possible to make up the target amount, return $-1$.
|
||||
|
||||
The greedy strategy adopted in this problem is shown in the following figure. Given the target amount, **we greedily choose the coin that is closest to and not greater than it**, repeatedly following this step until the target amount is met.
|
||||
The greedy strategy adopted in this problem is shown in Figure 15-1. Given the target amount, **we greedily choose the coin that is closest to and not greater than it**, repeatedly following this step until the target amount is met.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -330,7 +330,7 @@ You might exclaim: So clean! The greedy algorithm solves the coin change problem
|
||||
|
||||
**Greedy algorithms are not only straightforward and simple to implement, but they are also usually very efficient**. In the code above, if the smallest coin denomination is $\min(coins)$, the greedy choice loops at most $amt / \min(coins)$ times, giving a time complexity of $O(amt / \min(coins))$. This is an order of magnitude smaller than the time complexity of the dynamic programming solution, which is $O(n \times amt)$.
|
||||
|
||||
However, **for some combinations of coin denominations, greedy algorithms cannot find the optimal solution**. The following figure provides two examples.
|
||||
However, **for some combinations of coin denominations, greedy algorithms cannot find the optimal solution**. Figure 15-2 provides two examples.
|
||||
|
||||
- **Positive example $coins = [1, 5, 10, 20, 50, 100]$**: In this coin combination, given any $amt$, the greedy algorithm can find the optimal solution.
|
||||
- **Negative example $coins = [1, 20, 50]$**: Suppose $amt = 60$, the greedy algorithm can only find the combination $50 + 1 \times 10$, totaling 11 coins, but dynamic programming can find the optimal solution of $20 + 20 + 20$, needing only 3 coins.
|
||||
|
||||
@@ -10,7 +10,7 @@ comments: true
|
||||
|
||||
The capacity of the container is the product of the height and the width (area), where the height is determined by the shorter partition, and the width is the difference in array indices between the two partitions.
|
||||
|
||||
Please select two partitions in the array that maximize the container's capacity and return this maximum capacity. An example is shown in the following figure.
|
||||
Please select two partitions in the array that maximize the container's capacity and return this maximum capacity. An example is shown in Figure 15-7.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -28,13 +28,13 @@ Assuming the length of the array is $n$, the number of combinations of two parti
|
||||
|
||||
### 1. Determination of a greedy strategy
|
||||
|
||||
There is a more efficient solution to this problem. As shown in the following figure, we select a state $[i, j]$ where the indices $i < j$ and the height $ht[i] < ht[j]$, meaning $i$ is the shorter partition, and $j$ is the taller one.
|
||||
There is a more efficient solution to this problem. As shown in Figure 15-8, we select a state $[i, j]$ where the indices $i < j$ and the height $ht[i] < ht[j]$, meaning $i$ is the shorter partition, and $j$ is the taller one.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 15-8 Initial state </p>
|
||||
|
||||
As shown in the following figure, **if we move the taller partition $j$ closer to the shorter partition $i$, the capacity will definitely decrease**.
|
||||
As shown in Figure 15-9, **if we move the taller partition $j$ closer to the shorter partition $i$, the capacity will definitely decrease**.
|
||||
|
||||
This is because when moving the taller partition $j$, the width $j-i$ definitely decreases; and since the height is determined by the shorter partition, the height can only remain the same (if $i$ remains the shorter partition) or decrease (if the moved $j$ becomes the shorter partition).
|
||||
|
||||
@@ -42,7 +42,7 @@ This is because when moving the taller partition $j$, the width $j-i$ definitely
|
||||
|
||||
<p align="center"> Figure 15-9 State after moving the taller partition inward </p>
|
||||
|
||||
Conversely, **we can only possibly increase the capacity by moving the shorter partition $i$ inward**. Although the width will definitely decrease, **the height may increase** (if the moved shorter partition $i$ becomes taller). For example, in the Figure 15-10 , the area increases after moving the shorter partition.
|
||||
Conversely, **we can only possibly increase the capacity by moving the shorter partition $i$ inward**. Although the width will definitely decrease, **the height may increase** (if the moved shorter partition $i$ becomes taller). For example, in Figure 15-10, the area increases after moving the shorter partition.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -50,7 +50,7 @@ Conversely, **we can only possibly increase the capacity by moving the shorter p
|
||||
|
||||
This leads us to the greedy strategy for this problem: initialize two pointers at the ends of the container, and in each round, move the pointer corresponding to the shorter partition inward until the two pointers meet.
|
||||
|
||||
The following figures illustrate the execution of the greedy strategy.
|
||||
Figure 15-11 illustrate the execution of the greedy strategy.
|
||||
|
||||
1. Initially, the pointers $i$ and $j$ are positioned at the ends of the array.
|
||||
2. Calculate the current state's capacity $cap[i, j]$ and update the maximum capacity.
|
||||
@@ -415,7 +415,7 @@ The variables $i$, $j$, and $res$ use a constant amount of extra space, **thus t
|
||||
|
||||
The reason why the greedy method is faster than enumeration is that each round of greedy selection "skips" some states.
|
||||
|
||||
For example, under the state $cap[i, j]$ where $i$ is the shorter partition and $j$ is the taller partition, greedily moving the shorter partition $i$ inward by one step leads to the "skipped" states shown below. **This means that these states' capacities cannot be verified later**.
|
||||
For example, under the state $cap[i, j]$ where $i$ is the shorter partition and $j$ is the taller partition, greedily moving the shorter partition $i$ inward by one step leads to the "skipped" states shown in Figure 15-12. **This means that these states' capacities cannot be verified later**.
|
||||
|
||||
$$
|
||||
cap[i, i+1], cap[i, i+2], \dots, cap[i, j-2], cap[i, j-1]
|
||||
|
||||
@@ -38,7 +38,7 @@ n & \geq 4
|
||||
\end{aligned}
|
||||
$$
|
||||
|
||||
As shown below, when $n \geq 4$, splitting out a $2$ increases the product, **which indicates that integers greater than or equal to $4$ should be split**.
|
||||
As shown in Figure 15-14, when $n \geq 4$, splitting out a $2$ increases the product, **which indicates that integers greater than or equal to $4$ should be split**.
|
||||
|
||||
**Greedy strategy one**: If the splitting scheme includes factors $\geq 4$, they should be further split. The final split should only include factors $1$, $2$, and $3$.
|
||||
|
||||
@@ -48,7 +48,7 @@ As shown below, when $n \geq 4$, splitting out a $2$ increases the product, **wh
|
||||
|
||||
Next, consider which factor is optimal. Among the factors $1$, $2$, and $3$, clearly $1$ is the worst, as $1 \times (n-1) < n$ always holds, meaning splitting out $1$ actually decreases the product.
|
||||
|
||||
As shown below, when $n = 6$, $3 \times 3 > 2 \times 2 \times 2$. **This means splitting out $3$ is better than splitting out $2$**.
|
||||
As shown in Figure 15-15, when $n = 6$, $3 \times 3 > 2 \times 2 \times 2$. **This means splitting out $3$ is better than splitting out $2$**.
|
||||
|
||||
**Greedy strategy two**: In the splitting scheme, there should be at most two $2$s. Because three $2$s can always be replaced by two $3$s to obtain a higher product.
|
||||
|
||||
@@ -65,7 +65,7 @@ From the above, the following greedy strategies can be derived.
|
||||
|
||||
### 2. Code implementation
|
||||
|
||||
As shown below, we do not need to use loops to split the integer but can use the floor division operation to get the number of $3$s, $a$, and the modulo operation to get the remainder, $b$, thus:
|
||||
As shown in Figure 15-16, we do not need to use loops to split the integer but can use the floor division operation to get the number of $3$s, $a$, and the modulo operation to get the remainder, $b$, thus:
|
||||
|
||||
$$
|
||||
n = 3a + b
|
||||
|
||||
Reference in New Issue
Block a user