Show problems as
Classic problem #539MediumArrayMathStringSorting

Minimum Time Difference

Real-world version: Closest Two Times on the Clock
Tracked in this browser only
Read Minimum Time Difference on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Clock times are given as timePoints, each written as "HH:MM" on a twenty-four hour clock.

Return the fewest minutes between any two of the times, treating the clock as running round: the gap from one time to another may be measured either way about midnight, and the smaller is taken.

Examples

Example 1

Input
timePoints = ["07:42", "19:03", "07:55", "02:18"]
Output
13

Sorted, the times run 02:18, 07:42, 07:55 and 19:03. The closest pair is 07:42 and 07:55, thirteen minutes apart.

Example 2

Input
timePoints = ["22:30", "01:15"]
Output
165

Going forwards from 22:30 to 01:15 is two hours and forty-five minutes, which is shorter than going the other way round the clock.

Example 3

Input
timePoints = ["13:13", "13:13"]
Output
0

The same time appears twice, so the gap between them is nothing.

Constraints

  • 2 <= timePoints.length <= 2 * 10^4
  • timePoints[i] is written as "HH:MM" on a twenty-four hour clock

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def find_min_difference(timePoints: list[str]) -> int:
Java
public int findMinDifference(List<String> timePoints)