##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1904.html

# 1904. The Number of Full Rounds You Have Played

Medium

## Description

A new online video game has been released, and in this video game, there are 15-minute rounds scheduled every quarter-hour period. This means that at HH:00, HH:15, HH:30 and HH:45, a new round starts, where HH represents an integer number from 00 to 23. A 24-hour clock is used, so the earliest time in the day is 00:00 and the latest is 23:59.

Given two strings startTime and finishTime in the format "HH:MM" representing the exact time you started and finished playing the game, respectively, calculate the number of full rounds that you played during your game session.

• For example, if startTime = "05:20" and finishTime = "05:59" this means you played only one full round from 05:30 to 05:45. You did not play the full round from 05:15 to 05:30 because you started after the round began, and you did not play the full round from 05:45 to 06:00 because you stopped before the round ended.

If finishTime is earlier than startTime, this means you have played overnight (from startTime to the midnight and from midnight to finishTime).

Return the number of full rounds that you have played if you had started playing at startTime and finished at finishTime.

Example 1:

Input: startTime = “12:01”, finishTime = “12:44”

Output: 1

Explanation: You played one full round from 12:15 to 12:30.

You did not play the full round from 12:00 to 12:15 because you started playing at 12:01 after it began.

You did not play the full round from 12:30 to 12:45 because you stopped playing at 12:44 before it ended.

Example 2:

Input: startTime = “20:00”, finishTime = “06:00”

Output: 40

Explanation: You played 16 full rounds from 20:00 to 00:00 and 24 full rounds from 00:00 to 06:00.

16 + 24 = 40.

Example 3:

Input: startTime = “00:00”, finishTime = “23:59”

Output: 95

Explanation: You played 4 full rounds each hour except for the last hour where you played 3 full rounds.

Constraints:

• startTime and finishTime are in the format HH:MM.
• 00 <= HH <= 23
• 00 <= MM <= 59
• startTime and finishTime are not equal.

## Solution

If the finish time is less than the start time, add 24 hours to the finish time. Next find the smallest time greater than or equal to startTime that has minutes as a multiple of 15, and find the largest time less than or equal to finishTime that has minutes as a multiple of 15. Then calculate the time range between the start time and the end time. Divide the time range by 15 to get the number of full rounds. The number of full rounds must be non-negative, so if the calculated number of full rounds is negative, return 0.

• class Solution {
public int numberOfRounds(String startTime, String finishTime) {
int startHour = Integer.parseInt(startTime.substring(0, 2)), startMinute = Integer.parseInt(startTime.substring(3));
int finishHour = Integer.parseInt(finishTime.substring(0, 2)), finishMinute = Integer.parseInt(finishTime.substring(3));
if (finishHour < startHour || finishHour == startHour && finishMinute < startMinute)
finishHour += 24;
if (startMinute % 15 != 0) {
startMinute = startMinute - startMinute % 15 + 15;
if (startMinute == 60) {
startHour++;
startMinute = 0;
}
}
if (finishMinute % 15 != 0)
finishMinute -= finishMinute % 15;
int minutesBetween = (finishHour - startHour) * 60 + (finishMinute - startMinute);
minutesBetween = Math.max(minutesBetween, 0);
int rounds = minutesBetween / 15;
return rounds;
}
}

############

class Solution {
public int numberOfRounds(String startTime, String finishTime) {
int start = get(startTime), finish = get(finishTime);
if (start > finish) {
finish += 24 * 60;
}
start = (start + 14) / 15;
finish /= 15;
return Math.max(0, finish - start);
}

private int get(String s) {
return Integer.parseInt(s.substring(0, 2)) * 60 + Integer.parseInt(s.substring(3));
}
}

• class Solution:
def numberOfRounds(self, startTime: str, finishTime: str) -> int:
def get(s: str) -> int:
return int(s[:2]) * 60 + int(s[3:])

start, finish = get(startTime), get(finishTime)
if start > finish:
finish += 24 * 60
start, finish = (start + 14) // 15, finish // 15
return max(0, finish - start)

############

# 1904. The Number of Full Rounds You Have Played
# https://leetcode.com/problems/the-number-of-full-rounds-you-have-played/

class Solution:
def numberOfRounds(self, startTime: str, finishTime: str) -> int:
stt = startTime.split(":")
ftt = finishTime.split(":")
st = int("".join(stt))
ft = int("".join(ftt))

mm = 0
overnight = (int(ftt) <= int(stt) and int(ftt) <= int(stt))

if overnight:
h = 24 - int(stt)
h += int(ftt)

if h > 0:
h -= 1
mm += h * 4
hh = (int(stt) + h) % 24
stt = "0" + str(hh) if hh < 10 else str(hh)

def getNext(x):
if 0 <= x < 15: return "15"
if 15 <= x < 30: return "30"
if 30 <= x < 45: return "45"

return "00"

while int(stt) != int(ftt) or int(stt) != int(ftt):
if int(stt) == int(ftt) and int(stt) + 15 > int(ftt): break

if stt in ["00", "15", "30", "45"]:
mm += 1

nxt = getNext(int(stt))
stt = nxt
if stt == "00":
stt = str(int(stt) + 1) if int(stt) < 23 else "00"

return mm


• class Solution {
public:
int numberOfRounds(string startTime, string finishTime) {
int start = get(startTime), finish = get(finishTime);
if (start > finish) {
finish += 24 * 60;
}
start = (start + 14) / 15;
finish /= 15;
return max(0, finish - start);
}

private:
int get(string s) {
int a, b;
sscanf(s.c_str(), "%d:%d", &a, &b);
return a * 60 + b;
}
};

• func numberOfRounds(startTime string, finishTime string) int {
start, finish := get(startTime), get(finishTime)
if start > finish {
finish += 24 * 60
}
start = (start + 14) / 15
finish /= 15
if start > finish {
return 0
}
return finish - start

}

func get(s string) int {
var a, b int
fmt.Sscanf(s, "%d:%d", &a, &b)
return a*60 + b
}

• function numberOfRounds(startTime: string, finishTime: string): number {
let m1 = toMinutes(startTime),
m2 = toMinutes(finishTime);
if (m1 > m2) {
m2 += 24 * 60;
}
let ans = Math.floor(m2 / 15) - Math.ceil(m1 / 15);
return ans > 0 ? ans : 0;
}

function toMinutes(time: string): number {
let [h, m] = time.split(':').map(Number);
return h * 60 + m;
}