---
# System prepended metadata

title: 0539. Minimum Time Difference
tags: [Leetcode, Sorting, Bucket Sort, Medium]

---

# 0539. Minimum Time Difference
###### tags: `Leetcode` `Medium` `Sorting` `Bucket Sort`
Link: https://leetcode.com/problems/minimum-time-difference/
## 思路 $O(N)$
桶排序，因为最大的取值范围就是24\*60
## Code
```java=
class Solution {
    public int findMinDifference(List<String> timePoints) {
        boolean[] time = new boolean[24*60];
        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
        for(String timePoint:timePoints){
            String[] split = timePoint.split(":");
            int h = Integer.parseInt(split[0]);
            int m = Integer.parseInt(split[1]);
            if(time[h*60+m]) return 0;
            time[h*60+m] = true;
            min = Math.min(min, h*60+m);
            max = Math.max(max, h*60+m);
        }
        
        int ans = 24*60+min-max;
        int prev = Integer.MIN_VALUE;
        
        for(int i=0;i<time.length;i++){
            if(time[i]){
                if(prev!=Integer.MIN_VALUE){
                    ans=Math.min(ans, i-prev);
                }
                prev=i;
            }
        }
        return ans;
    }
}
```