* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
5.6 KiB
Max Capacity Problem
!!! question
Input an array $ht$, where each element represents the height of a vertical partition. Any two partitions in the array, along with the space between them, can form a container.
The capacity of the container equals the product of height and 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 such that the capacity of the formed container is maximized, and return the maximum capacity. An example is shown in the figure below.
The container is formed by any two partitions, therefore the state of this problem is the indices of two partitions, denoted as $[i, j]$.
According to the problem description, capacity equals height multiplied by width, where height is determined by the shorter partition, and width is the difference in array indices between the two partitions. Let the capacity be cap[i, j], then the calculation formula is:
cap[i, j] = \min(ht[i], ht[j]) \times (j - i)
Let the array length be n, then the number of combinations of two partitions (total number of states) is C_n^2 = \frac{n(n - 1)}{2}. Most directly, we can exhaustively enumerate all states to find the maximum capacity, with time complexity O(n^2).
Greedy Strategy Determination
This problem has a more efficient solution. As shown in the figure below, select a state [i, j] where index i < j and height ht[i] < ht[j], meaning i is the short partition and j is the long partition.
As shown in the figure below, if we now move the long partition j closer to the short partition i, the capacity will definitely decrease.
This is because after moving the long partition j, the width j-i definitely decreases; and since height is determined by the short partition, the height can only remain unchanged (i is still the short partition) or decrease (the moved j becomes the short partition).
Conversely, we can only possibly increase capacity by contracting the short partition i inward. Because although width will definitely decrease, height may increase (the moved short partition i may become taller). For example, in the figure below, the area increases after moving the short partition.
From this we can derive the greedy strategy for this problem: initialize two pointers at both ends of the container, and in each round contract the pointer corresponding to the short partition inward, until the two pointers meet.
The figure below shows the execution process of the greedy strategy.
- In the initial state, pointers
iandjare at both ends of the array. - Calculate the capacity of the current state
cap[i, j], and update the maximum capacity. - Compare the heights of partition
iand partitionj, and move the short partition inward by one position. - Loop through steps
2.and3.untiliandjmeet.
Code Implementation
The code loops at most n rounds, therefore the time complexity is $O(n)$.
Variables i, j, and res use a constant amount of extra space, therefore the space complexity is $O(1)$.
[file]{max_capacity}-[class]{}-[func]{max_capacity}
Correctness Proof
The reason greedy is faster than exhaustive enumeration is that each round of greedy selection "skips" some states.
For example, in state cap[i, j] where i is the short partition and j is the long partition, if we greedily move the short partition i inward by one position, the states shown in the figure below will be "skipped". This means that the capacities of these states cannot be verified later.
cap[i, i+1], cap[i, i+2], \dots, cap[i, j-2], cap[i, j-1]
Observing carefully, these skipped states are actually all the states obtained by moving the long partition j inward. We have already proven that moving the long partition inward will definitely decrease capacity. That is, the skipped states cannot possibly be the optimal solution, skipping them will not cause us to miss the optimal solution.
The above analysis shows that the operation of moving the short partition is "safe", and the greedy strategy is effective.













