Checking every pair of posts is O(n²). The two pointers idea is to start with the widest container and then only move the pointer that is limiting the result, because moving the other one can never help.
A row of upright posts stands at positions 0, 1, 2, and so on; height[i] is the height of the post at position i. Stretch a waterproof sheet between any two posts i < j to make a rain tank: the water can rise only as high as the shorter post, across the gap between them, so the tank holds (j - i) * min(height[i], height[j]). Return the most water any pair of posts can hold.
Examples
Input: height = [3, 7, 2, 5, 4, 8, 1, 6]
Output: 36
Explanation: Posts at index 1 (height 7) and index 7 (height 6): width 6 * height 6 = 36.
Input: height = [2, 5]
Output: 2
Constraints
2 <= len(height) <= 10**40 <= height[i] <= 10**4- Aim for O(n) time.
Goals
- Start with the widest possible pair and shrink inward
- Justify which pointer to move by reasoning about what could possibly improve the answer
- Replace an O(n²) pair enumeration with an O(n) two-pointer scan