Skip to content

15.3   Maximum capacity problem

Question

Given an array \(ht\), each element represents the height of a vertical wall. Any two walls, together with the space between them, form a container.

The container's capacity is its height multiplied by its width (an area). Its height is determined by the shorter wall, and its width is the difference between the two walls' array indices.

Choose two walls from the array to maximize the container's capacity, and return that maximum capacity. An example is shown in Figure 15-7.

Example input for the maximum capacity problem

Figure 15-7   Example input for the maximum capacity problem

A container is formed by two walls, so the state of this problem is the pair of wall indices, denoted by \([i, j]\).

Capacity is height times width, with the height determined by the shorter wall and the width equal to the difference between the wall indices. Writing the capacity as \(cap[i, j]\), we have:

\[ cap[i, j] = \min(ht[i], ht[j]) \times (j - i) \]

For an array of length \(n\), there are \(C_n^2 = \frac{n(n - 1)}{2}\) pairs of walls (possible states). The most direct approach is to enumerate all states and find the maximum capacity, taking \(O(n^2)\) time.

1.   Choosing a greedy strategy

A more efficient solution is possible. Consider the state \([i, j]\) in Figure 15-8, where \(i < j\) and \(ht[i] < ht[j]\): wall \(i\) is shorter and wall \(j\) is taller.

Initial state

Figure 15-8   Initial state

As shown in Figure 15-9, moving the taller wall \(j\) inward toward the shorter wall \(i\) must reduce the capacity.

Moving wall \(j\) inward always reduces the width \(j-i\). Since the shorter wall determines the height, the height can only stay the same (if \(i\) remains shorter) or decrease (if the new wall \(j\) is shorter).

State after moving the taller wall inward

Figure 15-9   State after moving the taller wall inward

Conversely, only moving the shorter wall \(i\) inward can potentially increase the capacity. Although the width must decrease, the height may increase if the new wall \(i\) is taller. Figure 15-10 shows an example where moving the shorter wall increases the area.

State after moving the shorter wall inward

Figure 15-10   State after moving the shorter wall inward

This gives us a greedy strategy: initialize two pointers at opposite ends of the container. At each step, move the pointer at the shorter wall inward until the pointers meet.

Figure 15-11 illustrates the greedy process.

  1. Initially, pointers \(i\) and \(j\) are at opposite ends of the array.
  2. Calculate the current capacity \(cap[i, j]\) and update the maximum capacity.
  3. Compare the heights of walls \(i\) and \(j\), and move the shorter wall inward by one position.
  4. Repeat steps 2. and 3. until \(i\) and \(j\) meet.

Greedy steps for the maximum capacity problem

max_capacity_greedy_step2

max_capacity_greedy_step3

max_capacity_greedy_step4

max_capacity_greedy_step5

max_capacity_greedy_step6

max_capacity_greedy_step7

max_capacity_greedy_step8

max_capacity_greedy_step9

Figure 15-11   Greedy steps for the maximum capacity problem

2.   Code implementation

The loop runs at most \(n\) times, so the time complexity is \(O(n)\).

The variables \(i\), \(j\), and \(res\) require constant extra space, so the space complexity is \(O(1)\).

max_capacity.py
def max_capacity(ht: list[int]) -> int:
    """Maximum container capacity: greedy algorithm"""
    # Initialize i and j at opposite ends of the array
    i, j = 0, len(ht) - 1
    # The initial maximum capacity is 0
    res = 0
    # Repeatedly make greedy choices until the two sides meet
    while i < j:
        # Update the maximum capacity
        cap = min(ht[i], ht[j]) * (j - i)
        res = max(res, cap)
        # Move the shorter side inward
        if ht[i] < ht[j]:
            i += 1
        else:
            j -= 1
    return res
max_capacity.cpp
/* Maximum container capacity: greedy algorithm */
int maxCapacity(vector<int> &ht) {
    // Initialize i and j at opposite ends of the array
    int i = 0, j = ht.size() - 1;
    // The initial maximum capacity is 0
    int res = 0;
    // Repeatedly make greedy choices until the two sides meet
    while (i < j) {
        // Update the maximum capacity
        int cap = min(ht[i], ht[j]) * (j - i);
        res = max(res, cap);
        // Move the shorter side inward
        if (ht[i] < ht[j]) {
            i++;
        } else {
            j--;
        }
    }
    return res;
}
max_capacity.java
/* Maximum container capacity: greedy algorithm */
int maxCapacity(int[] ht) {
    // Initialize i and j at opposite ends of the array
    int i = 0, j = ht.length - 1;
    // The initial maximum capacity is 0
    int res = 0;
    // Repeatedly make greedy choices until the two sides meet
    while (i < j) {
        // Update the maximum capacity
        int cap = Math.min(ht[i], ht[j]) * (j - i);
        res = Math.max(res, cap);
        // Move the shorter side inward
        if (ht[i] < ht[j]) {
            i++;
        } else {
            j--;
        }
    }
    return res;
}
max_capacity.cs
/* Maximum container capacity: greedy algorithm */
int MaxCapacity(int[] ht) {
    // Initialize i and j at opposite ends of the array
    int i = 0, j = ht.Length - 1;
    // The initial maximum capacity is 0
    int res = 0;
    // Repeatedly make greedy choices until the two sides meet
    while (i < j) {
        // Update the maximum capacity
        int cap = Math.Min(ht[i], ht[j]) * (j - i);
        res = Math.Max(res, cap);
        // Move the shorter side inward
        if (ht[i] < ht[j]) {
            i++;
        } else {
            j--;
        }
    }
    return res;
}
max_capacity.go
/* Maximum container capacity: greedy algorithm */
func maxCapacity(ht []int) int {
    // Initialize i and j at opposite ends of the array
    i, j := 0, len(ht)-1
    // The initial maximum capacity is 0
    res := 0
    // Repeatedly make greedy choices until the two sides meet
    for i < j {
        // Update the maximum capacity
        capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)
        res = int(math.Max(float64(res), float64(capacity)))
        // Move the shorter side inward
        if ht[i] < ht[j] {
            i++
        } else {
            j--
        }
    }
    return res
}
max_capacity.swift
/* Maximum container capacity: greedy algorithm */
func maxCapacity(ht: [Int]) -> Int {
    // Initialize i and j at opposite ends of the array
    var i = ht.startIndex, j = ht.endIndex - 1
    // The initial maximum capacity is 0
    var res = 0
    // Repeatedly make greedy choices until the two sides meet
    while i < j {
        // Update the maximum capacity
        let cap = min(ht[i], ht[j]) * (j - i)
        res = max(res, cap)
        // Move the shorter side inward
        if ht[i] < ht[j] {
            i += 1
        } else {
            j -= 1
        }
    }
    return res
}
max_capacity.js
/* Maximum container capacity: greedy algorithm */
function maxCapacity(ht) {
    // Initialize i and j at opposite ends of the array
    let i = 0,
        j = ht.length - 1;
    // The initial maximum capacity is 0
    let res = 0;
    // Repeatedly make greedy choices until the two sides meet
    while (i < j) {
        // Update the maximum capacity
        const cap = Math.min(ht[i], ht[j]) * (j - i);
        res = Math.max(res, cap);
        // Move the shorter side inward
        if (ht[i] < ht[j]) {
            i += 1;
        } else {
            j -= 1;
        }
    }
    return res;
}
max_capacity.ts
/* Maximum container capacity: greedy algorithm */
function maxCapacity(ht: number[]): number {
    // Initialize i and j at opposite ends of the array
    let i = 0,
        j = ht.length - 1;
    // The initial maximum capacity is 0
    let res = 0;
    // Repeatedly make greedy choices until the two sides meet
    while (i < j) {
        // Update the maximum capacity
        const cap: number = Math.min(ht[i], ht[j]) * (j - i);
        res = Math.max(res, cap);
        // Move the shorter side inward
        if (ht[i] < ht[j]) {
            i += 1;
        } else {
            j -= 1;
        }
    }
    return res;
}
max_capacity.dart
/* Maximum container capacity: greedy algorithm */
int maxCapacity(List<int> ht) {
  // Initialize i and j at opposite ends of the array
  int i = 0, j = ht.length - 1;
  // The initial maximum capacity is 0
  int res = 0;
  // Repeatedly make greedy choices until the two sides meet
  while (i < j) {
    // Update the maximum capacity
    int cap = min(ht[i], ht[j]) * (j - i);
    res = max(res, cap);
    // Move the shorter side inward
    if (ht[i] < ht[j]) {
      i++;
    } else {
      j--;
    }
  }
  return res;
}
max_capacity.rs
/* Maximum container capacity: greedy algorithm */
fn max_capacity(ht: &[i32]) -> i32 {
    // Initialize i and j at opposite ends of the array
    let mut i = 0;
    let mut j = ht.len() - 1;
    // The initial maximum capacity is 0
    let mut res = 0;
    // Repeatedly make greedy choices until the two sides meet
    while i < j {
        // Update the maximum capacity
        let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;
        res = std::cmp::max(res, cap);
        // Move the shorter side inward
        if ht[i] < ht[j] {
            i += 1;
        } else {
            j -= 1;
        }
    }
    res
}
max_capacity.c
/* Maximum container capacity: greedy algorithm */
int maxCapacity(int ht[], int htLength) {
    // Initialize i and j at opposite ends of the array
    int i = 0;
    int j = htLength - 1;
    // The initial maximum capacity is 0
    int res = 0;
    // Repeatedly make greedy choices until the two sides meet
    while (i < j) {
        // Update the maximum capacity
        int capacity = myMin(ht[i], ht[j]) * (j - i);
        res = myMax(res, capacity);
        // Move the shorter side inward
        if (ht[i] < ht[j]) {
            i++;
        } else {
            j--;
        }
    }
    return res;
}
max_capacity.kt
/* Maximum container capacity: greedy algorithm */
fun maxCapacity(ht: IntArray): Int {
    // Initialize i and j at opposite ends of the array
    var i = 0
    var j = ht.size - 1
    // The initial maximum capacity is 0
    var res = 0
    // Repeatedly make greedy choices until the two sides meet
    while (i < j) {
        // Update the maximum capacity
        val cap = min(ht[i], ht[j]) * (j - i)
        res = max(res, cap)
        // Move the shorter side inward
        if (ht[i] < ht[j]) {
            i++
        } else {
            j--
        }
    }
    return res
}
max_capacity.rb
### Maximum container capacity: greedy algorithm ###
def max_capacity(ht)
  # Initialize i and j at opposite ends of the array
  i, j = 0, ht.length - 1
  # The initial maximum capacity is 0
  res = 0

  # Repeatedly make greedy choices until the two sides meet
  while i < j
    # Update the maximum capacity
    cap = [ht[i], ht[j]].min * (j - i)
    res = [res, cap].max
    # Move the shorter side inward
    if ht[i] < ht[j]
      i += 1
    else
      j -= 1
    end
  end

  res
end
Visualize execution

3.   Proof of correctness

The greedy algorithm is faster than exhaustive search because each greedy choice skips some states.

For example, in state \(cap[i, j]\), suppose \(i\) is the shorter wall and \(j\) the taller one. Moving the shorter wall \(i\) inward by one position skips the states shown in Figure 15-12. Their capacities will never be checked later.

\[ cap[i, i+1], cap[i, i+2], \dots, cap[i, j-2], cap[i, j-1] \]

States skipped by moving the shorter wall

Figure 15-12   States skipped by moving the shorter wall

Observe that the skipped states are precisely those obtained by moving the taller wall \(j\) inward. We have already shown that moving the taller wall inward must reduce capacity. None of these skipped states can therefore be optimal, and skipping them cannot cause us to miss the optimal solution.

This shows that moving the shorter wall is safe, and that the greedy strategy is valid.

Share your insights, questions, or suggestions in the comments