#1904

The Number of Full Rounds You Have Played

Medium
MathStringMathematical calculations with time intervalsHandling edge cases with time formats
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(1)
Space
O(1)
O(1)
💡

Intuition

Time O(1)Space O(1)

The optimal solution calculates the total number of rounds directly by leveraging the properties of time intervals, which allows us to avoid iterating through each minute.

⚙️

Algorithm

4 steps
  1. 1Step 1: Convert loginTime and logoutTime into minutes since midnight.
  2. 2Step 2: If logoutTime is earlier than loginTime, adjust logoutTime to account for crossing midnight.
  3. 3Step 3: Calculate the number of full rounds before loginTime and after logoutTime.
  4. 4Step 4: Calculate the total number of rounds played by subtracting the two counts.
solution.py21 lines
1# Full working Python code
2
3def countFullRounds(loginTime, logoutTime):
4    def timeToMinutes(t):
5        h, m = map(int, t.split(':'))
6        return h * 60 + m
7
8    login = timeToMinutes(loginTime)
9    logout = timeToMinutes(logoutTime)
10
11    if logout < login:
12        logout += 24 * 60  # Adjust for crossing midnight
13
14    # Calculate full rounds
15    roundsBeforeLogin = (login // 15)
16    roundsAfterLogout = (logout // 15)
17
18    return roundsAfterLogout - roundsBeforeLogin
19
20# Example usage
21print(countFullRounds('09:31', '10:14'))  # Output: 1

Complexity note: The complexity is O(1) because we are performing a constant number of operations regardless of the input size.

  • 1Understanding how to convert time into a single unit (minutes) simplifies calculations.
  • 2Recognizing when to adjust for crossing midnight is crucial for accurate results.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.