Problem Description
Given two times, loginTime and logoutTime (in hh:mm format), your task is to determine how many complete chess rounds have been played during an online chess tournament. Each round lasts 15 minutes, starting at 00:00. If logoutTime is earlier than loginTime, it indicates that the session spans midnight.
Key Insights
- Convert the time strings to total minutes since midnight.
- Adjust the logout time by adding 1440 minutes (24 hours) if it is earlier than the login time.
- Round up the loginTime to the next multiple of 15 since the round started in progress is not counted.
- Round down the logoutTime to the previous multiple of 15 because a round is only complete if it finishes before logout.
- The difference between these adjusted times, divided by 15, gives the total number of full rounds played.
Space and Time Complexity
Time Complexity: O(1) — All operations are constant time. Space Complexity: O(1) — Only a few integer variables are used.
Solution
First, we convert the login and logout times into minutes since midnight. If the logout time is earlier, add 1440 minutes to account for the day rollover. Then, round the login time up to the nearest multiple of 15 and the logout time down to the nearest multiple of 15. The full rounds played are calculated as the difference between these adjusted times divided by 15.