Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Say we have a pair of special floors (x, y) with no other special floors in between. There are x - y - 1 consecutive floors in between them without a special floor.
Say there are n special floors. After sorting special, we have answer = max(answer, special[i] – special[i – 1] – 1) for all 0 < i < n.
However, there are two special cases left to consider: the floors before special[0] and after special[n-1].
To consider these cases, we have answer = max(answer, special[0] – bottom, top – special[n-1]).