Wednesday, October 18, 2017

539. Minimum Time Difference

https://leetcode.com/problems/minimum-time-difference/description/
    int findMinDifference(vector<string>& timePoints) {
        sort(timePoints.begin(), timePoints.end());
        int h0 = stoi(timePoints.back().substr(0,2));
        int m0 = stoi(timePoints.back().substr(3));
        int h1, m1, dif, res = 12*60;
        for(auto &s : timePoints) {
            h1 = stoi(s.substr(0,2));
            m1 = stoi(s.substr(3));
            dif = abs(h1*60+m1 - (h0*60+m0));  
            if(dif>12*60) dif = 24*60 - dif;
            if(dif<res) res = dif;
            h0 = h1;
            m0 = m1;
        }
        return res;
    }

No comments:

Post a Comment