Advertisement

The Number of Full Rounds You Have Played - LeetCode 1904 Solution

The Number of Full Rounds You Have Played - Complete Solution Guide

The Number of Full Rounds You Have Played is LeetCode problem 1904, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 logoutTim

Detailed Explanation

The problem asks us to calculate the number of complete 15-minute chess rounds a player participated in, given their login and logout times. A chess round starts every 15 minutes, beginning at 00:00. If the logout time is earlier than the login time, it implies the player played across midnight. The inputs are two strings, `loginTime` and `logoutTime`, in the format 'hh:mm'. The output is an integer representing the number of full rounds played.

Solution Approach

The solution converts the given login and logout times into total minutes from the start of the day (00:00). It then addresses the scenario where the game spans across midnight by adding 24 hours worth of minutes to the logout time if it is earlier than the login time. Next, it calculates the number of full rounds that can be played, considering the login and logout times. This is done by finding the first possible complete 15-minute round *after* the login time, and the last completed round *before* the logout time. The difference between these represents the number of full rounds played.

Step-by-Step Algorithm

  1. Step 1: Parse the `loginTime` and `logoutTime` strings to extract the hours and minutes as integers.
  2. Step 2: Convert the login and logout times to minutes from the start of the day (00:00).
  3. Step 3: If the `logout_minutes` is less than `login_minutes`, add 24 * 60 (1440) to `logout_minutes` to account for playing across midnight.
  4. Step 4: Calculate the `start_quarter`. This is the index of the first possible full round the player could participate in. Add 14 to login_minutes and integer divide by 15 to find this quarter. The addition ensures we find the next *complete* round after the login.
  5. Step 5: Calculate the `end_quarter`. This is the index of the last completed full round the player participated in. Integer divide logout_minutes by 15 to find the last completed quarter.
  6. Step 6: Calculate the number of full rounds played by subtracting `start_quarter` from `end_quarter`.
  7. Step 7: Return the number of full rounds played, ensuring the result is not negative (using `max(0, num_rounds)`).

Key Insights

  • Insight 1: Convert login and logout times into minutes to simplify calculations.
  • Insight 2: Handle the case where the logout time is earlier than the login time, indicating play across midnight, by adding 24 hours (1440 minutes) to the logout time.
  • Insight 3: Determine the starting and ending 'quarters' (15-minute intervals) based on login and logout times and calculate the difference.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: Math, String.

Companies

Asked at: General Motors.