leetcode - 539. 最小时间差
题目:
代码:
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
int getMin(string str){
int tn = ((str[0]-'0')*10 + (str[1] - '0'))*60 + (str[3]-'0')*10 + (str[4] - '0');
return tn;
}
int findMinDifference(vector<string> &timePoints) {
int n = timePoints.size();
sort(timePoints.begin(), timePoints.end());
int start = getMin(timePoints[0]);
int pre = start;
int res = INT_MAX;
int tn;
for(int i = 1; i < n; i++){
int cur = getMin(timePoints[i]);
tn = cur - pre;
pre = cur;
res = min(res, tn);
}
res = min(res, start + 1440 - pre);
return res;
}
int main(){
int n, res;
cin>>n;
vector<string> timePoints(n, "");
for(int i = 0; i < n; i++){
cin>>timePoints[i];
}
res = findMinDifference(timePoints);
cout<<res;
return 0;
}