Skip to content

Commit 55c6624

Browse files
authored
Create minimum-time-difference.cpp
1 parent 0a4ae0e commit 55c6624

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

C++/minimum-time-difference.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int findMinDifference(vector<string>& timePoints) {
7+
static const int N = 60 * 24;
8+
vector<int> minutes;
9+
for (const auto& t : timePoints) {
10+
minutes.emplace_back(stoi(t.substr(0, 2)) * 60 + stoi(t.substr(3)));
11+
}
12+
sort(minutes.begin(), minutes.end());
13+
int result = numeric_limits<int>::max();
14+
for (int i = 0; i < timePoints.size(); ++i) {
15+
result = min(result, (N + minutes[(i + 1) % timePoints.size()] - minutes[i]) % N);
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)