Problem

Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list.

Example

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

Solution


public class Solution {
    /**
     * @param timePoints: a list of 24-hour clock time points
     * @return: the minimum minutes difference between any two time points in the list
     */
    public int findMinDifference(List<String> timePoints) {
        // Write your code here
        List<Integer> timesInMinute = new ArrayList<>();
        for (String str: timePoints) {
            Integer minutes = Integer.valueOf(str.substring(0, 2)) * 60 + Integer.valueOf(str.substring(3, 5));
            timesInMinute.add(minutes);
        }
        Collections.sort(timesInMinute);
        int minDiff = 1440;
        for (int i = 1; i < timesInMinute.size(); i++) {
            minDiff = Math.min(minDiff, timesInMinute.get(i) - timesInMinute.get(i-1));
        }
        minDiff = Math.min(minDiff, 1440 + timesInMinute.get(0) - timesInMinute.get(timesInMinute.size()-1));
        return minDiff;
    }
}

linspiration
161 声望53 粉丝

引用和评论

0 条评论