The Number of Full Rounds You Have Played
Problem
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 at00:45, and the seventh round starts at01:30.
You are given two strings loginTime and logoutTime where:
loginTimeis the time you will login to the game, andlogoutTimeis 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.
Examples
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
loginTimeandlogoutTimeare in the formathh:mm.00 <= hh <= 2300 <= mm <= 59loginTimeandlogoutTimeare not equal.
Solution
Method 1 - Convert to Minutes and Count Full 15-Minute Intervals
We convert both times to minutes, handle the overnight case, and count the number of full 15-minute intervals between the next round after login and the previous round before logout.
Code
C++
#include <string>
using namespace std;
class Solution {
public:
int numberOfRounds(string loginTime, string logoutTime) {
int start = stoi(loginTime.substr(0,2)) * 60 + stoi(loginTime.substr(3,2));
int end = stoi(logoutTime.substr(0,2)) * 60 + stoi(logoutTime.substr(3,2));
if (end < start) end += 24*60;
int s = (start + 14) / 15 * 15;
int e = end / 15 * 15;
return max(0, (e - s) / 15);
}
};
Java
class Solution {
public int numberOfRounds(String loginTime, String logoutTime) {
int start = Integer.parseInt(loginTime.substring(0,2)) * 60 + Integer.parseInt(loginTime.substring(3,5));
int end = Integer.parseInt(logoutTime.substring(0,2)) * 60 + Integer.parseInt(logoutTime.substring(3,5));
if (end < start) end += 24*60;
int s = ((start + 14) / 15) * 15;
int e = (end / 15) * 15;
return Math.max(0, (e - s) / 15);
}
}
Python
class Solution:
def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
def to_minutes(t):
h, m = map(int, t.split(':'))
return h * 60 + m
start = to_minutes(loginTime)
end = to_minutes(logoutTime)
if end < start:
end += 24*60
s = ((start + 14) // 15) * 15
e = (end // 15) * 15
return max(0, (e - s) // 15)
Rust
impl Solution {
pub fn number_of_rounds(login_time: String, logout_time: String) -> i32 {
fn to_minutes(t: &str) -> i32 {
let h = t[0..2].parse::<i32>().unwrap();
let m = t[3..5].parse::<i32>().unwrap();
h * 60 + m
}
let start = to_minutes(&login_time);
let mut end = to_minutes(&logout_time);
if end < start { end += 24*60; }
let s = ((start + 14) / 15) * 15;
let e = (end / 15) * 15;
(e - s).max(0) / 15
}
}
TypeScript
function numberOfRounds(loginTime: string, logoutTime: string): number {
function toMinutes(t: string): number {
const [h, m] = t.split(':').map(Number);
return h * 60 + m;
}
let start = toMinutes(loginTime);
let end = toMinutes(logoutTime);
if (end < start) end += 24*60;
const s = Math.ceil((start) / 15) * 15;
const e = Math.floor(end / 15) * 15;
return Math.max(0, (e - s) / 15);
}
Complexity
- ⏰ Time complexity:
O(1) - 🧺 Space complexity:
O(1)