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

原文链接: https://www.cnblogs.com/dysjtu1995/p/12704088.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    539. Minimum Time Difference

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/342251

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午1:30
下一篇 2023年3月2日 上午1:30

相关推荐