LeetCode Problem Workspace
The Number of Full Rounds You Have Played
Solve this math plus string problem to calculate the number of full rounds played in a chess tournament between login and logout times.
2
Topics
5
Code langs
3
Related
Practice Focus
Medium · Math plus String
Answer-first summary
Solve this math plus string problem to calculate the number of full rounds played in a chess tournament between login and logout times.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Math plus String
To solve this problem, you need to determine the number of full chess rounds played between two given times. The rounds start every 15 minutes, and the challenge is to calculate how many complete rounds were played between loginTime and logoutTime. You must handle cases where the login and logout times span across midnight.
Problem Statement
In an online chess tournament, rounds start every 15 minutes. The first round begins at 00:00, and each subsequent round starts at 15-minute intervals throughout the day. You're given two times, loginTime and logoutTime, in the format hh:mm, where loginTime is when you log in, and logoutTime is when you log out.
Your task is to calculate how many full rounds were played between loginTime and logoutTime. If logoutTime is earlier than loginTime, it means you played from loginTime to midnight, then from midnight to logoutTime. Ensure that you properly account for the span across midnight when determining the full rounds played.
Examples
Example 1
Input: loginTime = "09:31", logoutTime = "10:14"
Output: 1
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
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.
Solution Approach
Treat the Day as 48 Hours
Instead of working within a 24-hour format, treat the day as a 48-hour period. This helps when logoutTime is earlier than loginTime, as you can easily handle the transition from one day to the next.
Calculate Time Increments
The key is to break the time into 15-minute intervals. For each of these intervals, calculate whether the interval is completely played between the given loginTime and logoutTime. Use basic math to find how many such full intervals occur.
Handle Midnight Crossings
Account for the possibility that the time spans across midnight, i.e., logoutTime is earlier than loginTime. This can be done by converting the times into minutes since midnight and then handling the transition properly for a complete calculation.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity depends on the final approach, but typically involves constant-time calculations based on simple math and comparisons. The space complexity is minimal, involving only a few integer variables for storing intermediate calculations.
What Interviewers Usually Probe
- Ensure the candidate handles edge cases like spanning midnight correctly.
- Look for clear, methodical handling of time and intervals in the solution.
- Evaluate the candidate’s understanding of time manipulations and boundary conditions.
Common Pitfalls or Variants
Common pitfalls
- Failing to account for the transition across midnight, leading to incorrect results.
- Not recognizing that the day can be treated as 48 hours instead of 24, which simplifies the logic.
- Incorrectly handling the last partial round before logoutTime if it's not fully completed.
Follow-up variants
- Consider scenarios where both loginTime and logoutTime are within the same day.
- Test for edge cases with times just before or after midnight, e.g., 23:59 and 00:00.
- Alter the round duration to something other than 15 minutes and calculate the new number of rounds.
FAQ
How do I handle loginTime and logoutTime crossing midnight in this problem?
Treat the day as 48 hours, where times after midnight are simply an extension of the previous day. This simplifies the calculation for spanning midnight.
What is the key pattern for solving 'The Number of Full Rounds You Have Played'?
The problem combines math with string manipulation, specifically working with time intervals and handling edge cases around midnight.
What should I do if the time intervals don't align perfectly with 15-minute rounds?
You should only count complete rounds. If the interval doesn’t fit a full 15-minute slot, it should not be included in the count.
How can I efficiently calculate the number of full rounds?
Break the total time into 15-minute intervals and check whether each full round fits between loginTime and logoutTime, handling time correctly for boundary conditions.
Can this problem be solved without converting times into minutes?
It’s possible, but converting times to minutes simplifies the problem, making calculations easier, especially when crossing midnight.
Solution
Solution 1: Convert to Minutes
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$.
class Solution:
def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
def f(s: str) -> int:
return int(s[:2]) * 60 + int(s[3:])
a, b = f(loginTime), f(logoutTime)
if a > b:
b += 1440
a, b = (a + 14) // 15, b // 15
return max(0, b - a)Continue Topic
math
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Math plus String
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Medium
Stay on this level to stabilize interview delivery.
arrow_forward