BugForge
DSA

Largest Storage Between Racks

Mediumtwo pointersgreedy 00:00

Rack heights stand in a row at unit spacing. Pick two racks; the storage between them is min(h[i], h[j]) × (j - i). Return the maximum storage.

Signature

maxStorage(heights: int[]) → int
Example 1
heights = [1,8,6,2,5,4,8,3,7]
output = 49
racks 1 and 8: min(8,7) × 7 = 49
Example 2
heights = [1,1]
output = 1

Constraints

  • 2 ≤ n ≤ 10^5
  • 0 ≤ heights[i] ≤ 10^4
Run checks the examples. Submit runs every hidden test (17, including large inputs that catch slow solutions).