← All problems

539. Minimum Time Difference

MediumOpen on LeetCodeProblem statement

Problem Statement

539. Minimum Time Difference

Medium


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:

Java

Source file
class Solution {
    public int findMinDifference(List<String> timePoints) {
        int n = timePoints.size(), minDiff = Integer.MAX_VALUE;
        int[] timeInMins = new int[n];
        for (int i = 0; i < n; i++) {
            timeInMins[i] = Integer.parseInt(timePoints.get(i).substring(0,2)) * 60 + Integer.parseInt(timePoints.get(i).substring(3));
        }
        Arrays.sort(timeInMins);
        for (int i = 1; i < n; i++)
            minDiff = Math.min(minDiff, timeInMins[i] - timeInMins[i - 1]);
        return minDiff = Math.min(minDiff, 24 * 60 - timeInMins[n - 1] + timeInMins[0]);
    }
}