0539.cpp (1437B)
1 class Solution { 2 static const int day = 24 * 60; 3 4 static inline int val(const char *time) { return (time[0] & 0xF) * 10 + (time[1] & 0xF); } 5 6 static inline int val(const string &time) { return val(time.c_str()) * 60 + val(time.c_str() + 3); } 7 8 public: 9 int findMinDifference(const vector<string> &timePoints) { 10 const int n = size(timePoints); 11 vector<int> time(n); 12 13 for (int i = 0; i < n; i++) 14 time[i] = val(timePoints[i]); 15 sort(begin(time), end(time)); 16 17 int res = day - time.back() + time.front(); 18 for (int i = 1; i < n; i++) 19 res = min(res, time[i] - time[i - 1]); 20 21 return res; 22 } 23 }; 24 25 // No extra memory, little bit slower 26 class Solution { 27 static const int day = 24 * 60; 28 29 static inline int val(const char *time) { return (time[0] & 0xF) * 10 + (time[1] & 0xF); } 30 31 static inline int val(const string &time) { return val(time.c_str()) * 60 + val(time.c_str() + 3); } 32 33 public: 34 int findMinDifference(vector<string> &timePoints) { 35 const int n = size(timePoints); 36 sort(begin(timePoints), end(timePoints)); 37 38 int res = INT_MAX; 39 int first = val(timePoints[0]), prev = first; 40 for (int i = 1; i < n; i++) { 41 const int crnt = val(timePoints[i]); 42 res = min(res, crnt - prev); 43 prev = crnt; 44 } 45 46 return min(res, first + (day - prev)); 47 } 48 };