We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

The Number of Full Rounds You Have Played

Difficulty: Medium


Problem Description

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. 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. Return the number of full chess rounds you have played in the tournament.


Key Insights

  • Chess rounds start at fixed intervals (every 15 minutes) throughout the day.
  • If logoutTime is earlier than loginTime, the gameplay extends to midnight and then resumes until logoutTime.
  • We need to determine the number of full rounds played, which requires identifying the rounds that fully encompass the login and logout times.

Space and Time Complexity

Time Complexity: O(1)
Space Complexity: O(1)


Solution

To solve the problem, we need to convert the loginTime and logoutTime into minutes from the start of the day. This will allow us to easily calculate the number of rounds played. The steps are as follows:

  1. Convert both loginTime and logoutTime into total minutes since midnight.
  2. If logoutTime is less than loginTime, calculate the rounds played from loginTime to midnight and then from midnight to logoutTime.
  3. Determine the nearest upcoming round start time after loginTime and the nearest previous round start time before logoutTime.
  4. Count the number of full rounds between these two times.

Code Solutions

def countRoundsPlayed(loginTime: str, logoutTime: str) -> int:
    def timeToMinutes(t: str) -> int:
        hh, mm = map(int, t.split(':'))
        return hh * 60 + mm
    
    login_minutes = timeToMinutes(loginTime)
    logout_minutes = timeToMinutes(logoutTime)
    
    # If logout is earlier than login, we need to account for midnight
    if logout_minutes < login_minutes:
        logout_minutes += 24 * 60
    
    # Find the first full round after loginTime
    first_round_start = (login_minutes + 14) // 15 * 15
    # Find the last full round before logoutTime
    last_round_start = logout_minutes // 15 * 15
    
    # Count the number of full rounds
    if first_round_start > last_round_start:
        return 0
    return (last_round_start - first_round_start) // 15 + 1
← Back to All Questions