[Leetcode 539] Minimum Time Difference (Medium)

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 1:

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

Note:

  1. The number of time points in the given list is at least 2 and won't exceed 20000.
  2. The input time is legal and ranges from 00:00 to 23:59.

Intuitive Solution 1

  • 每两个输入时间两两比较,得到最小的时差
  • Time: O(n^2). n * (n - 1)个比较

Improved Solution 2

  • 对输入List排序
  • 再对排序后的列表,前后两个相邻的进行比较即可: 因为排序后,非相邻的差值一定不是最小的。

Time:
- Sort: O (nLogn) + Compare O (n)

  • 排序代价大,因此考虑在排序上进行优化

Optimized Solution

  • 可以借鉴 counting sort的思路,优化排序
  • 对于时间排序,其范围是固定的,00:00 ~ 23:59。所以可以用一个长度为 60 * 24list of boolean来模拟hash table,记录所有这些输入的时间。
  • list的每一个ENTRY就代表一个时间点:遍历input string list,将每个时间转换成minutes, 其代表list index, 在对应的index写入true从而记录时间点
  • 然后再两两比较前后2个有值的index, index差值就是minimum time difference
  • 注意:
    1. 如果输入的string list其大小大于60 * 24,说明至少有2个时间是重复得,那么最小差值就是0.
    2. 在遍历string list写入hash table,发现该位置已经有值了,那么说明出现了重复时间,那么最小差值也是0.
    3. 注意00:02, 23:59之间的最小差值是3,这种情况需特殊处理。Diff = First Time in List + Time Scope - Last Time in List.
class Solution {
    public int findMinDifference(List timePoints) {
        int MAX_TIME_RANGE = 24 * 60;
        int timePointsLength = timePoints.size ();
        
        // There must be two time entries which are identical
        if (timePointsLength > MAX_TIME_RANGE)
        {
            return 0;
        }
        
        // A hashtable to store the timePoints in minutes
        boolean[] tracker = new boolean [MAX_TIME_RANGE];
        
        for (String timePoint : timePoints)
        {
            String[] temp = timePoint.split (":");
            int timeInMinutes = Integer.parseInt (temp [0]) * 60 + Integer.parseInt (temp [1]);
            
            // Matches an existing timePoint, => there are idential => min difference is 0
            if (tracker [timeInMinutes])
            {
                return 0;
            }
            tracker [timeInMinutes] = true;
        }
        
        return findMinDifference (tracker);
    }
    
    public int findMinDifference (boolean[] tracker)
    {
        int prev = -1;
        int minDiff = Integer.MAX_VALUE;
        
        // the first and last timePoint in the sorted timePoints list
        int firstTimePoint = Integer.MAX_VALUE;
        int lastTimePoint = Integer.MIN_VALUE;
        
        for (int i = 0; i < tracker.length; i++)
        {
            if (!tracker [i])
            {
                continue;
            }
            
            if (prev >= 0)
            {
                minDiff = Math.min (i - prev, minDiff);
            }
            
            prev = i;
            
            firstTimePoint = Math.min (firstTimePoint, i);
            lastTimePoint = Math.max (lastTimePoint, i);  
        }
        
        // Because the time is continuously, 23:59 -> 00:00 -> 00:01
        // Thus, if given timepoints are: 23:00, 23:59, 00:02
        // the minDifference is actually 3 (23:59, 00:02) =>  00:02 + 24:00 - 23:59 = 3
        return Math.min (minDiff, firstTimePoint + tracker.length - lastTimePoint);
    }
}

你可能感兴趣的:([Leetcode 539] Minimum Time Difference (Medium))