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.
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, andlogoutTime
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
.
Example 1:
Input: loginTime = "09:31", logoutTime = "10:14" Output: 1 Explanation: 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.
Example 2:
Input: loginTime = "21:30", logoutTime = "03:00" Output: 22 Explanation: You played 10 full rounds from 21:30 to 00:00 and 12 full rounds from 00:00 to 03:00. 10 + 12 = 22.
Constraints:
loginTime
and logoutTime
are in the format hh:mm
.00 <= hh <= 23
00 <= mm <= 59
loginTime
and logoutTime
are not equal.This problem asks to calculate the number of 15-minute chess rounds played given login and logout times. The challenge lies in handling cases where the logout time is before the login time (crossing midnight).
The most efficient approach involves these steps:
Convert to Minutes: Transform both loginTime
and logoutTime
strings into total minutes past midnight. This simplifies the round calculation.
Handle Midnight Crossing: If logoutTime
(in minutes) is less than loginTime
(in minutes), it signifies a midnight crossing. Add 1440 (minutes in a day) to logoutTime
to represent the total playing time across midnight.
Round Calculation:
loginTime
(in minutes) to the nearest 15-minute interval. This accounts for only considering full rounds after the login. We use ceiling because you only start counting rounds after you login.logoutTime
(in minutes) to the nearest 15-minute interval. This accounts for only considering full rounds before the logout. We use floor since we only count rounds before logging out.loginTime
from the rounded-down logoutTime
. This gives the number of 15-minute intervals (full rounds) played.Handle Zero or Negative Rounds: Ensure the result is not negative. If the difference is negative (meaning the player played less than a full round), return 0.
class Solution:
def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
def to_minutes(time_str: str) -> int:
hours, minutes = map(int, time_str.split(':'))
return hours * 60 + minutes
login_minutes = to_minutes(loginTime)
logout_minutes = to_minutes(logoutTime)
if logout_minutes < login_minutes:
logout_minutes += 1440 # Account for midnight crossing
#Round to nearest 15 minute intervals
rounded_login = (login_minutes + 14) // 15 * 15 # Ceiling division for login
rounded_logout = logout_minutes // 15 * 15 # Floor division for logout
full_rounds = (rounded_logout - rounded_login) // 15
return max(0, full_rounds) #Handle cases where less than a full round is played
The implementations in other languages (Java, C++, Go, TypeScript) follow the same logic, only differing in syntax and function names. The core algorithm remains consistent for optimal efficiency.