{x}
blog image

Minimum Time Difference

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".

Solution Explanation: Minimum Time Difference

The problem asks to find the minimum difference in minutes between any two time points in a given list. The time points are provided in "HH:MM" format.

Approach:

The most efficient approach involves sorting and clever handling of the circular nature of time (midnight bridging).

  1. Handle Duplicates: If the number of time points exceeds 1440 (the number of minutes in a day), there must be duplicate times, resulting in a minimum difference of 0. This optimization avoids unnecessary processing.

  2. Convert to Minutes: Each time point ("HH:MM") is converted into its total minute representation (hours * 60 + minutes). This simplifies the difference calculation.

  3. Sort: The list of minute representations is sorted in ascending order. Sorting allows us to efficiently find the minimum difference between adjacent elements.

  4. Circular Difference: To account for the difference between the last time point and the first (crossing midnight), we append the first time point plus 1440 minutes (a full day) to the sorted list. This ensures that the minimum difference across the midnight boundary is considered.

  5. Find Minimum Difference: Iterate through the sorted list and calculate the difference between consecutive elements. The minimum of these differences is the answer.

Time Complexity: O(n log n), dominated by the sorting step.

Space Complexity: O(n) in the worst case, to store the converted minute representations. This can be improved to O(1) if we modify the input array inplace.

Code Implementation (Python):

from itertools import pairwise
 
class Solution:
    def findMinDifference(self, timePoints: List[str]) -> int:
        if len(timePoints) > 1440:
            return 0
        nums = sorted(int(x[:2]) * 60 + int(x[3:]) for x in timePoints)
        nums.append(nums[0] + 1440)
        return min(b - a for a, b in pairwise(nums))
 

Explanation of Python Code:

  • pairwise(nums): This uses the itertools library to efficiently generate pairs of consecutive elements from the sorted nums list. This is a concise way to compute the differences.
  • List Comprehension: sorted(int(x[:2]) * 60 + int(x[3:]) for x in timePoints) efficiently converts each time string to minutes and sorts them simultaneously.

Code Implementation (Java):

import java.util.*;
class Solution {
    public int findMinDifference(List<String> timePoints) {
        if (timePoints.size() > 1440) {
            return 0;
        }
        int n = timePoints.size();
        int[] nums = new int[n + 1];
        for (int i = 0; i < n; ++i) {
            String[] t = timePoints.get(i).split(":");
            nums[i] = Integer.parseInt(t[0]) * 60 + Integer.parseInt(t[1]);
        }
        Arrays.sort(nums, 0, n);
        nums[n] = nums[0] + 1440;
        int ans = 1 << 30; // Initialize with a large value
        for (int i = 1; i <= n; ++i) {
            ans = Math.min(ans, nums[i] - nums[i - 1]);
        }
        return ans;
    }
}

Explanation of Java Code:

  • Arrays.sort(nums, 0, n): Sorts only the first n elements of the nums array, leaving space for the added element.
  • ans = 1 << 30;: This efficiently initializes ans to a large value (greater than any possible difference).

The other language implementations follow a similar structure, adapting the syntax and libraries accordingly. The core algorithm remains consistent across all solutions.