其他分享
首页 > 其他分享> > Leetcode 539. 最小时间差 (直接做O(n^2), 贪心排序优化到O(nlogn)

Leetcode 539. 最小时间差 (直接做O(n^2), 贪心排序优化到O(nlogn)

作者:互联网

 

class Solution {
public:
    int findMinDifference(vector<string>& timePoints) {
        vector<int> minutes;
        for (auto timePoint: timePoints) {
            int hour = stoi(timePoint.substr(0, 2));
            int minute = stoi(timePoint.substr(3));
            minutes.push_back(hour * 60 + minute);
        }
        sort(minutes.begin(), minutes.end());
        minutes.push_back(minutes[0] + 24 * 60);
        int res = INT_MAX;
        for (int i = 1; i < minutes.size(); i++) {
            int t = minutes[i] - minutes[i - 1];
            res = min(res, t);
        }
        return res;
    }
};

标签:int,res,timePoint,60,substr,nlogn,minutes,Leetcode,539
来源: https://blog.csdn.net/wwxy1995/article/details/119491378