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.
Input: loginTime ="09:31", logoutTime ="10:14"Output: 1Explanation: You played one full round from 09:45 to 10:00.You did not play the full round from 09:30 to 09:45 because you logged in at 09:31 after it began.You did not play the full round from 10:00 to 10:15 because you logged out at 10:14 before it ended.
Input: loginTime ="21:30", logoutTime ="03:00"Output: 22Explanation: You played 10 full rounds from 21:30 to 00:00 and 12 full rounds from 00:00 to 03:00.10+12=22.
Method 1 - Convert to Minutes and Count Full 15-Minute Intervals#
We convert both times to minutes, handle the overnight case, and count the number of full 15-minute intervals between the next round after login and the previous round before logout.
#include<string>usingnamespace std;
classSolution {
public:int numberOfRounds(string loginTime, string logoutTime) {
int start = stoi(loginTime.substr(0,2)) *60+ stoi(loginTime.substr(3,2));
int end = stoi(logoutTime.substr(0,2)) *60+ stoi(logoutTime.substr(3,2));
if (end < start) end +=24*60;
int s = (start +14) /15*15;
int e = end /15*15;
returnmax(0, (e - s) /15);
}
};
1
2
3
4
5
6
7
8
9
10
classSolution {
publicintnumberOfRounds(String loginTime, String logoutTime) {
int start = Integer.parseInt(loginTime.substring(0,2)) * 60 + Integer.parseInt(loginTime.substring(3,5));
int end = Integer.parseInt(logoutTime.substring(0,2)) * 60 + Integer.parseInt(logoutTime.substring(3,5));
if (end < start) end += 24*60;
int s = ((start + 14) / 15) * 15;
int e = (end / 15) * 15;
return Math.max(0, (e - s) / 15);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defnumberOfRounds(self, loginTime: str, logoutTime: str) -> int:
defto_minutes(t):
h, m = map(int, t.split(':'))
return h *60+ m
start = to_minutes(loginTime)
end = to_minutes(logoutTime)
if end < start:
end +=24*60 s = ((start +14) //15) *15 e = (end //15) *15return max(0, (e - s) //15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnnumber_of_rounds(login_time: String, logout_time: String) -> i32 {
fnto_minutes(t: &str) -> i32 {
let h = t[0..2].parse::<i32>().unwrap();
let m = t[3..5].parse::<i32>().unwrap();
h *60+ m
}
let start = to_minutes(&login_time);
letmut end = to_minutes(&logout_time);
if end < start { end +=24*60; }
let s = ((start +14) /15) *15;
let e = (end /15) *15;
(e - s).max(0) /15 }
}