Coding problem
The two panels that hold the most water
The task
Glass panels of different heights stand in a row, one metre apart. Any two of them, with the panels between them taken away, make the walls of a tank, and the water can rise only as high as the shorter wall.
Complete MaxWater(int[] heights). heights[i] ≥ 0 is the height of the panel at position i. For two positions i < j, the water they hold is min(heights[i], heights[j]) × (j − i). Return the largest amount any pair holds; fewer than two panels give 0. Return a long.
Signature
public static long MaxWater(int[] heights)
Examples
[3, 1, 2]→4. The panels 3 and 2 are 2 apart and the shorter one is 2.[2, 5, 4, 3]→6. 2 and 3 (3 apart, height 2) and 5 and 3 (2 apart, height 3) both hold 6.
Limits: Up to 300,000 panels, each up to int.MaxValue high.
The tests check: fewer than two panels, equal heights, zero heights, tall panels at the ends, rising heights, a product beyond int, and 300,000 panels with the best pair in the middle.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public static class Solution
{
// Returns the most water two panels can hold: the shorter height times the distance.
public static long MaxWater(int[] heights)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.