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 <= 2300 <= mm <= 59loginTime and logoutTime are not equal.Problem Overview: You receive two timestamps in HH:MM format representing when a player logged in and logged out. A game round lasts exactly 15 minutes. The task is to count how many complete 15‑minute rounds were fully played between those two times. If the logout time is earlier than the login time, the session crossed midnight and continues into the next day.
Approach 1: Convert Times to Minutes and Calculate (Time: O(1), Space: O(1))
The straightforward strategy converts both timestamps into total minutes from midnight. Parse the string, compute hours * 60 + minutes, and handle overnight sessions by adding 24 * 60 minutes when the logout time is smaller than the login time. Once both values are normalized, round the start time up to the next 15‑minute boundary and round the end time down to the previous boundary. The number of full rounds equals (endRounded - startRounded) / 15 if the result is positive.
This approach works because a valid round must start and end exactly on multiples of 15 minutes. By shifting the login time upward and the logout time downward, only fully completed rounds remain in the interval. The implementation mainly uses simple arithmetic and string parsing, which makes it easy to implement in any language. Problems like this rely heavily on math operations combined with basic string parsing.
Approach 2: Utilize Modulo for Round Calculation (Time: O(1), Space: O(1))
This version performs the same logic but uses modulo arithmetic to align timestamps to valid round boundaries. After converting both times to minutes, adjust the login time using (15 - start % 15) % 15 to move it to the next multiple of 15. Adjust the logout time using end - (end % 15) to snap it to the previous boundary. Handle the overnight case by adding 1440 minutes when the logout time occurs the next day.
Once aligned, compute the difference and divide by 15 to get the total rounds. The modulo trick removes conditional rounding logic and keeps the calculation concise. This method is often preferred in interviews because it shows comfort with arithmetic transformations and edge cases such as midnight wrap‑around.
Recommended for interviews: Interviewers typically expect the minute‑conversion strategy combined with rounding to 15‑minute boundaries. It demonstrates clean reasoning about time intervals and edge cases like crossing midnight. The modulo variant is equally optimal and often considered a cleaner implementation once you recognize that every valid round aligns to multiples of 15.
To determine the number of full rounds played, first convert the login and logout times into total minutes from the start of the day. Then, calculate the number of 15-minute intervals (rounds) that fall completely within this range.
If the logout time is earlier than the login time, it means the session spans midnight and we need to account for the hours after midnight until the logout time.
This C program computes the total number of 15-minute chess rounds between loginTime and logoutTime. First, it converts time strings to total minutes. Next, it calculates and returns the number of complete rounds between these minutes, adjusting for midnight transition by adding 96 (the number of rounds in 24 hours) when necessary.
The time complexity is O(1) because we perform a fixed number of operations, and the space complexity is O(1) since we only use a constant amount of extra space.
In this approach, we make use of modulo operations to determine the start and end of each 15-minute interval and thus calculate the full intervals the session covered in a slightly different method. This calculation is crucial when dealing with transitions over midnight as seen in the problem description.
This C solution converts login and logout times to minutes properly accounting for wrapping using the modulo operator. The rounds are calculated based on division by 15 and adjusted for cross-day dive by adding 96 when necessary.
The complexity here remains O(1) for time and space as the amount of arithmetic operations does not scale with input size.
We can convert the input strings to minutes a and b. If a > b, it means that it crosses midnight, so we need to add one day's minutes 1440 to b.
Then we round a up to the nearest multiple of 15, and round b down to the nearest multiple of 15. Finally, we return the difference between b and a. Note that we should take the larger value between 0 and b - a.
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: Convert Times to Minutes and Calculate | The time complexity is O(1) because we perform a fixed number of operations, and the space complexity is O(1) since we only use a constant amount of extra space. |
| Approach 2: Utilize Modulo for Round Calculation | The complexity here remains O(1) for time and space as the amount of arithmetic operations does not scale with input size. |
| Convert to Minutes | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Convert Times to Minutes and Calculate | O(1) | O(1) | Best general solution. Easy to reason about and handles midnight edge cases clearly. |
| Modulo-Based Round Alignment | O(1) | O(1) | When you want concise arithmetic logic using modulo to align times to 15‑minute intervals. |
Leetcode Weekly Contest 246 | 1904. The Number of Full Rounds You Have Played in English • Competitive Somya • 742 views views
Watch 4 more video solutions →Practice The Number of Full Rounds You Have Played with our built-in code editor and test cases.
Practice on FleetCode