Skip to main content

539. Minimum Time Difference

Hint

class Solution {
public:
int findMinDifference(vector<string>& timePoints)
{
int n = timePoints.size();
vector<int> minutes(n);
for (int i = 0; i < n; i++)
{
int h = stoi(timePoints[i].substr(0, 2));
int m = stoi(timePoints[i].substr(3));
minutes[i] = (h * 60 + m);
}

sort(minutes.begin(), minutes.end());

int res = 1440;
for (int i = 0; i < n - 1; i++)
{
res = min(res, minutes[i + 1] - minutes[i]);
}
return min(res, 24 * 60 - (minutes[n - 1] - minutes[0]));
}
};
  • T: O()O()
  • S: O()O()