Backdrop Between Poles
A photographer works in a park with a row of wooden poles of different heights, exactly 1 metre apart. She wants to stretch one rectangular backdrop between two of them.
The backdrop is as wide as the gap between the poles and only as tall as the shorter one, or it sags. Poles in between do not matter, because the backdrop hangs in front of them. She wants the pair of poles that gives the biggest backdrop.
Your task
Write widestBackdrop(heights). heights[i] is the height of pole i. For two poles i < j, the backdrop area is (j - i) × min(heights[i], heights[j]). Return the largest possible area.
If there are fewer than two poles, return 0.
widestBackdrop(heights: int[]) → int
Examples
heights = [3, 7, 2, 6, 4, 8, 1]
28
Poles 1 (height 7) and 5 (height 8) are 4 metres apart, and the shorter is 7: 4 × 7 = 28. The two ends give only 6 × 1 = 6, because pole 6 is so short.
heights = [2, 10, 9, 1]
9
The tall neighbours 10 and 9 give 1 × 9 = 9. Wider pairs are held down by a short pole: 2 and 9 give 2 × 2 = 4, 2 and 1 give 3 × 1 = 3.
Constraints
- 0 ≤ heights.length ≤ 10,000
- 0 ≤ heights[i] ≤ 10,000
Start with brute force
Try every pair of poles and keep the largest area.
With 10,000 poles that is about 50 million pairs. Most are hopeless: a pair whose short pole was already paired with something farther away can never win, yet brute force checks it anyway.
Hints
0 of 4 shown- Hint 1
Start with the widest pair, the end poles of
[3, 7, 2, 6, 4, 8, 1]: area 6 × 1 = 6. The short right pole limits it. Could any other pair using that pole do better? - Hint 2
Any other pair with the height-1 pole is narrower and still at most 1 tall. That pole can never beat 6, so throw it away.
- Hint 3
This holds in general: of the two end poles, the shorter one has had its best chance.
- Hint 4
Put
leftat the first pole andrightat the last. Record the area, then move the pointer on the shorter pole one step inward. Repeat until they meet, keeping the largest area.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Start with the widest backdrop, the two end poles. Any other partner for the shorter one is closer, with the height still capped by that pole, so it can never beat this area. Drop it and move inward.
Trace [3, 7, 2, 6, 4, 8, 1]:
- Poles 0 and 6 (3 and 1): 6 × 1 = 6. Best = 6. Pole 6 is shorter, so move
rightto 5. - Poles 0 and 5 (3 and 8): 5 × 3 = 15. Best = 15. Pole 0 is shorter, so move
leftto 1. - Poles 1 and 5 (7 and 8): 4 × 7 = 28. Best = 28. Move
leftto 2. - Poles 2 and 5 (2 and 8): 3 × 2 = 6. Move
leftto 3. - Poles 3 and 5 (6 and 8): 2 × 6 = 12. Move
leftto 4. - Poles 4 and 5 (4 and 8): 1 × 4 = 4. Move
leftto 5. The pointers meet. Answer: 28.
Six pairs measured instead of all 21. The classic mistake is moving the taller pole: that throws away a pole that might still be in the best pair.
function widestBackdrop(heights) {
let left = 0, right = heights.length - 1;
let best = 0;
while (left < right) {
const area = (right - left) * Math.min(heights[left], heights[right]);
best = Math.max(best, area);
// The shorter pole cannot do better with any closer partner.
if (heights[left] < heights[right]) left++;
else right--;
}
return best;
}def widest_backdrop(heights):
left, right = 0, len(heights) - 1
best = 0
while left < right:
area = (right - left) * min(heights[left], heights[right])
best = max(best, area)
# The shorter pole cannot do better with any closer partner.
if heights[left] < heights[right]:
left += 1
else:
right -= 1
return bestRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.