Widest Gap Between Tall Walls
heights[i] is the height of the wall standing at position i. A wall is tall when its height is at least h. Return the largest distance j - i between two tall walls at positions i < j. If fewer than two walls are tall, return 0.
The widest gap always joins the leftmost tall wall to the rightmost tall wall, so one index can walk in from each end until it meets a tall wall.
Example 1
heights [3,1,4,1,5,9,2]
h 4
returns 3
The tall walls (at least 4) stand at positions 2, 4 and 5; the widest gap is 5 - 2 = 3.
Example 2
heights [1,2,3]
h 5
returns 0
No wall reaches 5.
Constraints
0 <= len(heights) <= 1000 <= heights[i] <= 10001 <= h <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.