The shortest gap
- O(n log n) · Hard
- Full plan
- Python
- JavaScript
- strings
- sorting
- division
Problem
Martha runs the north station, where trains come through day and night. To fit in the maintenance crew she needs the shortest time between two trains, and that count doesn't stop at midnight.
You get times, a list of strings "HH:MM" on a 24-hour clock, from "00:00" to "23:59", with at least two of them, in any order. Return an integer: the minutes between the two closest trains. Between two times, the shorter way counts, whether or not it goes through midnight: "23:55" and "00:10" are 15 minutes apart, not 1425. With "21:10", "06:45", "20:50" and "12:00" you return 20, for the 20:50 and the 21:10 trains. If two trains come through at the same time, return 0.
Examples
An ordinary day
["21:10", "06:45", "20:50", "12:00"] → 20
Before and after midnight
["23:55", "00:10"] → 15
Going around beats the neighbors
["00:05", "12:00", "23:50", "18:30"] → 15
The hour changes
["12:55", "13:05", "18:00"] → 10
Exactly twelve hours
["06:00", "18:00"] → 720
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def shortest_gap(times):
passJavaScript
function shortestGap(times) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.