mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-17 22:10:59 +00:00
Bug fixes and improvements (#1348)
* Add "reference" for EN version. Bug fixes. * Unify the figure reference as "the figure below" and "the figure above". Bug fixes. * Format the EN markdown files. * Replace "" with <u></u> for EN version and bug fixes * Fix biary_tree_dfs.png * Fix biary_tree_dfs.png * Fix zh-hant/biary_tree_dfs.png * Fix heap_sort_step1.png * Sync zh and zh-hant versions. * Bug fixes * Fix EN figures * Bug fixes * Fix the figure labels for EN version
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
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 the figure below.
|
||||
|
||||

|
||||
|
||||
@@ -22,11 +22,11 @@ Assuming the length of the array is $n$, the number of combinations of two parti
|
||||
|
||||
### 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 the figure below, 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.
|
||||
|
||||

|
||||
|
||||
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 the figure below, **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).
|
||||
|
||||
@@ -38,7 +38,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.
|
||||
The figure below 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.
|
||||
@@ -86,7 +86,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 the figure below. **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]
|
||||
|
||||
Reference in New Issue
Block a user