539. Minimum Time Difference

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 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.

思路

Solution (C++):

int findMinDifference(vector<string>& timePoints) {
    int n = timePoints.size();
    if (n < 2)  return 0;
    int min_diff = 24*60;
    
    vector<int> time(n, 0);
    for (int i = 0; i < n; ++i) {
        time[i] = stoi(timePoints[i].substr(0,2)) * 60 + stoi(timePoints[i].substr(3,2)); 
    }
    
    sort(time.begin(), time.end());   
    time.push_back(time[0] + min_diff);
    for (int i = 0; i < n; ++i) {
        min_diff = min(min_diff, time[i+1]-time[i]);
    }
    return min_diff;
}

性能

Runtime: 28 ms  Memory Usage: 9.7 MB

思路

Solution (C++):


性能

Runtime: ms  Memory Usage: MB

posted @ 2020-04-15 11:25  littledy  阅读(97)  评论(0)    收藏  举报