Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: The Number of Full Rounds You Have Played

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1904" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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. For example, the second round starts at 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, and logoutTime 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.

Explanation

Here's the solution to the chess rounds problem:

  • Core Idea: Convert login and logout times to minutes since the start of the day. Calculate the number of complete 15-minute intervals (chess rounds) within the logged-in period. Handle the case where the logout time is earlier than the login time (playing across midnight).
  • Round Calculation: For each segment of play (either login to logout directly, or login to midnight and then midnight to logout), determine the start time of the next round after the login time. Subtract this from the (appropriately rounded down) logout time to find the played minutes within the bounds, then divide by 15 to determine rounds played.
  • Midnight Handling: If the logout time is earlier than login time, split the calculation into two parts: from login time to 23:59, and from 00:00 to logout time.

  • Complexity: O(1) runtime, O(1) storage.

Code

    def numberOfRounds(loginTime: str, logoutTime: str) -> int:
    """
    Calculates the number of full chess rounds played in the tournament.

    Args:
        loginTime: The time you will login to the game.
        logoutTime: The time you will logout from the game.

    Returns:
        The number of full chess rounds you have played in the tournament.
    """

    def to_minutes(time: str) -> int:
        hours, minutes = map(int, time.split(':'))
        return hours * 60 + minutes

    login_minutes = to_minutes(loginTime)
    logout_minutes = to_minutes(logoutTime)

    if logout_minutes < login_minutes:
        total_rounds = calculate_rounds(login_minutes, 24 * 60) + calculate_rounds(0, logout_minutes)
    else:
        total_rounds = calculate_rounds(login_minutes, logout_minutes)

    return total_rounds

def calculate_rounds(start_minutes: int, end_minutes: int) -> int:
    """Calculates the number of full 15-minute rounds within a given time interval."""

    # Calculate the start of the next round after the login time
    start_round_minutes = (start_minutes + 14) // 15 * 15

    # If the start of the next round is after the end time, no rounds are played.
    if start_round_minutes > end_minutes:
        return 0

    # Calculate how many minutes have been played within the valid bounds
    played_minutes = end_minutes // 15 * 15 - start_round_minutes

    # Number of full 15-minute rounds
    return played_minutes // 15

More from this blog

C

Chatmagic blog

2894 posts