-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_time_difference.cpp
More file actions
64 lines (57 loc) · 1.61 KB
/
min_time_difference.cpp
File metadata and controls
64 lines (57 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* =====================================================================================
*
* Filename: min_time_difference.cpp
*
* Description: 539. Minimum Time Difference
*
* Version: 1.0
* Created: 11/10/2025 08:47:08
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <string>
#include <utility>
#include <vector>
#include "gtest/gtest.h"
class Solution {
public:
int findMinDifference(std::vector<std::string>& points) {
if (points.size() < 2) {
return 0;
}
auto convert = [](const std::string& s) -> int {
const int n = s.length();
if (n == 0 || n != 5 || s[2] != ':') {
return 0; // Invalid format
}
const int h = std::stoi(s.substr(0, 2));
const int m = std::stoi(s.substr(3, 2));
return (h * 60 + m);
};
std::vector<int> minutes(points.size());
for (int i = 0; i < points.size(); i++) {
minutes[i] = convert(points[i]);
}
std::sort(minutes.begin(), minutes.end());
int diff = minutes.front() + 24 * 60 - minutes.back();
for (int i = 1; i < minutes.size(); i++) {
diff = std::min(diff, minutes[i] - minutes[i - 1]);
}
return diff;
}
};
TEST(Solution, findMinDifference) {
std::vector<std::pair<std::vector<std::string>, int>> cases = {
{{"23:59", "00:00"}, 1},
{{"00:00", "23:59", "00:00"}, 0},
};
for (auto [points, diff] : cases) {
EXPECT_EQ(Solution().findMinDifference(points), diff);
}
}