---
# System prepended metadata

title: 'LeetCode #539 [Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/)'
tags: [LeetCode, Medium, String]

---

###### tags: `LeetCode` `Medium` `String`
# LeetCode #539 [Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/)
### (Medium)

給定一個 24 小時制（小時:分鐘 "HH:MM"）的時間列表，找出列表中任意兩個時間的最小時間差並以分鐘數表示。


---
排序整個timePoints, 然後兩兩相比, 最後再比頭尾, 並更新最小分鐘差。

---
```
class Solution {
public:
    int findMinDifference(vector<string>& timePoints) {
        int n=timePoints.size();
        sort(timePoints.begin(), timePoints.end());
        //提前發現有重複的, 不知道會不會比較快
        /*timePoints.erase(unique(timePoints.begin(), timePoints.end()), timePoints.end());
        if(timePoints.size()!=n)return 0;
        n = timePoints.size();*/
        int ans = INT_MAX;
        for(int i=1; i<n;i++){
            int h = (timePoints[i][0]-timePoints[i-1][0])*10+(timePoints[i][1]-timePoints[i-1][1]);
            int m = (timePoints[i][3]-timePoints[i-1][3])*10+(timePoints[i][4]-timePoints[i-1][4]);
            int tmp = h*60+m;
            ans = min(tmp, ans);
        }
        int h = (timePoints[0][0]-timePoints[n-1][0])*10+(timePoints[0][1]-timePoints[n-1][1]);
        int m = (timePoints[0][3]-timePoints[n-1][3])*10+(timePoints[0][4]-timePoints[n-1][4]);
        int tmp = h*60+m + 1440;
        ans = min(tmp, ans);
        return ans;
    }
};
```