Skip to main content

Minimum Time Difference - Solution & Explanation

MediumArrayMathStringSorting20 min readAsked at: Amazon, Microsoft, Meta +6
Practice this problem

Problem Statement

Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list.

 

Example 1:

Input: timePoints = ["23:59","00:00"]
Output: 1

Example 2:

Input: timePoints = ["00:00","23:59","00:00"]
Output: 0

 

Constraints:

  • 2 <= timePoints.length <= 2 * 104
  • timePoints[i] is in the format "HH:MM".

Approach Overview

Problem Overview: You receive a list of time points in HH:MM format. The task is to compute the smallest difference in minutes between any two time points. Because time wraps around midnight (e.g., 23:59 and 00:00), the comparison must also consider the circular nature of a 24‑hour clock.

Approach 1: Sorting and Finding Minimum Difference (O(n log n) time, O(n) space)

Convert each time string into total minutes from midnight (hour * 60 + minute). Store these values in an array and sort them. Once sorted, iterate through adjacent elements and compute the difference between each pair to track the minimum gap. The circular edge case is handled by comparing the last time of the day with the first time plus 1440 minutes. Sorting simplifies the comparison logic because the closest times must appear next to each other in the ordered list.

This method relies on basic operations from array processing and sorting. It works well for general input sizes and is easy to implement in most languages. Time complexity comes from sorting (O(n log n)), while the scan for differences runs in O(n). Space complexity is O(n) due to the converted minute array.

Approach 2: Using a Boolean Array to Track Minutes (O(n) time, O(1) space)

A day contains exactly 1440 minutes. Instead of sorting, allocate a boolean array of size 1440 and mark each minute that appears in the input. Convert every time string to its minute index and check if that index is already marked. If it is, the minimum difference is immediately 0 because duplicate times exist.

After marking all time points, scan the boolean array to locate consecutive marked minutes and compute their differences. Track the first and last seen minutes to correctly compute the circular difference across midnight. Because the array size is fixed (1440), the scan runs in constant time relative to the input size.

This technique uses ideas from array indexing and simple math transformations. The algorithm runs in O(n) time for inserting time points and O(1440) for scanning, which simplifies to O(n). Space complexity is effectively O(1) since the 1440‑length array is constant regardless of input size.

Recommended for interviews: The sorting approach is the most commonly expected solution because it is straightforward and demonstrates clean reasoning about ordering and edge cases like the midnight wrap‑around. Mentioning the boolean‑array optimization shows deeper insight: you recognize the bounded domain of minutes in a day and reduce the complexity to linear time. Showing both approaches signals strong problem‑solving depth in interviews.

Approach 1: Sorting and Finding Minimum Difference

Convert the time into minutes from "00:00", sort, then find the smallest difference between any two adjacent times while also considering the difference across midnight.

This implementation works by converting each time point to the number of minutes since 00:00. We sort these times and then calculate the adjacent differences. We also calculate the wraparound difference from the last to the first 24-hour period to ensure capturing the smallest difference across midnight.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n) due to sorting where n is the number of time points. Space Complexity: O(n) since we store the times in minutes.

Try this approach in the editor →

Approach 2: Using a Boolean Array to Track Minutes

Mark each minute of the day in a boolean array once any time point corresponds to it. Then traverse the array to find the smallest gap between marked minutes, considering wrap-around. This is efficient because it eliminates the need for sorting.

Here, a boolean array representing each minute of the day is used to track time points. The algorithm finds the smallest gap between the marked minutes, ensuring wrap-around difference is checked at the end.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n + M), where M is 1440, the number of minutes in a day, n is the length of list.
Space Complexity: O(M), M = 1440, fixed.

Try this approach in the editor →

Approach 3: Sorting

We notice that there can be at most 24 times 60 = 1440 distinct time points. Therefore, if the length of timePoints exceeds 1440, it implies there are duplicate time points, and we can return 0 early.

Next, we iterate through the list of time points and convert it into a list of minutes nums. For example, for the time point 13:14, we convert it into 13 times 60 + 14.

Then, we sort the list of minutes in ascending order and append the smallest time nums[0] plus 1440 to the end of the list. This step is to handle the special case of the difference between the maximum and minimum values.

Finally, we iterate through the list of minutes to find the minimum difference between any two adjacent times.

The time complexity is O(n log n), and the space complexity is O(n), where n is the number of time points.

Code

Python

Java

C++

Go

TypeScript

Rust

Swift

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Sorting and Finding Minimum Difference

Time Complexity: O(n log n) due to sorting where n is the number of time points. Space Complexity: O(n) since we store the times in minutes.

Using a Boolean Array to Track Minutes

Time Complexity: O(n + M), where M is 1440, the number of minutes in a day, n is the length of list.
Space Complexity: O(M), M = 1440, fixed.

Sorting

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Sorting and Finding Minimum DifferenceO(n log n)O(n)General solution that is simple to implement and commonly expected in interviews
Boolean Array for 1440 MinutesO(n)O(1)When exploiting the fixed 24‑hour domain for a faster linear-time solution

Video Solution

LeetCode 539. Minimum Time Difference (Solution Explained)Nick White13,691 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Time Difference easy or hard?
Minimum Time Difference is typically categorized as a Medium problem. The challenge comes from handling the circular 24‑hour clock and recognizing that the smallest difference may occur across midnight.
Minimum Time Difference Python/Java solution
In Python or Java, convert each time string using split(':') and compute total minutes as hour * 60 + minute. Either store these values in a list and sort them, or mark them in a 1440‑length boolean array for a linear‑time solution.
How to solve Minimum Time Difference in O(n)?
Use a boolean array of size 1440 representing every minute in a day. Convert each time string to its minute index and mark it in the array. Then scan the array to find the smallest gap between marked minutes and handle the wrap‑around difference between the last and first times.
What is the best approach for Minimum Time Difference?
The most common solution sorts all time points after converting them to minutes. Once sorted, compare adjacent times and also check the circular difference between the last and first values. This approach runs in O(n log n) time and is widely expected in coding interviews.
Is Minimum Time Difference asked at Google/Amazon/Meta?
Minimum Time Difference is a classic time‑conversion and sorting problem that appears in interview preparation sets for companies like Amazon and Google. It tests understanding of string parsing, array processing, and edge cases involving circular time ranges.
What data structure is used in Minimum Time Difference?
Common implementations use arrays to store minute values after converting the HH:MM strings. The optimized approach uses a boolean array of size 1440 to track whether a particular minute appears, avoiding the need for sorting.
What is the time complexity of Minimum Time Difference?
The standard sorting solution runs in O(n log n) time because the time points must be ordered before comparing neighbors. A more optimized method uses a fixed boolean array of 1440 minutes and runs in O(n) time with constant space.

Ready to solve this problem?

Practice Minimum Time Difference with our built-in code editor and test cases.

Practice on FleetCode