[LeetCode] The Number of Full Rounds You Have Played

1904. The Number of Full Rounds You Have Played

You are participating in an online chess tournament. There is a chess round that starts every 15 minutes. The first round of the day starts at 00:00, and after every 15 minutes, a new round starts.

  • For example, the second round starts at 00:15, the fourth round starts at 00:45, and the seventh round starts at 01:30.

You are given two strings loginTime and logoutTime where:

  • loginTime is the time you will login to the game, and
  • logoutTime is the time you will logout from the game.

If logoutTime is earlier than loginTime, this means you have played from loginTime to midnight and from midnight to logoutTime.

Return the number of full chess rounds you have played in the tournament.

Note: All the given times follow the 24-hour clock. That means the first round of the day starts at 00:00 and the last round of the day starts at 23:45.

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
class Solution {
pair<int, int> lowerBound(string s) {
int hour = stoi(s.substr(0,2));
int minute = stoi(s.substr(3));

if(minute < 15) minute = 0;
else if(minute < 30) minute = 15;
else if(minute < 45) minute = 30;
else minute = 45;

return {hour, minute};
}
pair<int, int> upperBound(string s) {
int hour = stoi(s.substr(0,2));
int minute = stoi(s.substr(3));

if(minute == 0) minute = 0;
else if(minute <= 15) minute = 15;
else if(minute <= 30) minute = 30;
else if(minute <= 45) minute = 45;
else {
minute = 0;
hour += 1;
if(hour == 24) hour = 0;
}
return {hour, minute};
}
bool fin(int sh, int sm, int eh, int em) {
if(sh == eh) return em == sm;
return false;
}
pair<int, int> parse(string s) {
int hour = stoi(s.substr(0,2));
int minute = stoi(s.substr(3));

return {hour, minute};
}
bool canPlay(string& s, string& e) {
auto [sh, sm] = parse(s);
auto [eh, em] = parse(e);
if(sh == eh) {
if(em < sm) return true;
return em - sm >= 15;
}
return true;
}
public:
int numberOfRounds(string loginTime, string logoutTime) {
if(!canPlay(loginTime, logoutTime)) return 0;
auto [sh, sm] = upperBound(loginTime);
auto [eh, em] = lowerBound(logoutTime);
int res = 0;
while(!fin(sh,sm,eh,em)) {
res += 1;
sm += 15;
if(sm == 60) {
sm = 0;
sh += 1;
}
if(sh == 24) sh = 0;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/05/PS/LeetCode/the-number-of-full-rounds-you-have-played/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.