LeetCode 题解工作台
你完成的完整对局数
一款新的在线电子游戏在近期发布,在该电子游戏中,以 刻钟 为周期规划若干时长为 15 分钟 的游戏对局。这意味着,在 HH:00 、 HH:15 、 HH:30 和 HH:45 ,将会开始一个新的对局,其中 HH 用一个从 00 到 23 的整数表示。游戏中使用 24 小时制的时钟 ,所以一天中最早…
2
题型
5
代码语言
3
相关题
当前训练重点
中等 · 数学·string
答案摘要
我们可以将输入的字符串转换为分钟数 和 ,如果 $a > b$,则说明跨越了午夜,需要将 加上一天的分钟数 。 然后我们将 向上取整到 的倍数,将 向下取整到 的倍数,最后返回 与 的差值即可,注意要取 和 $b - a$ 中的较大值。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数学·string 题型思路
题目描述
一款新的在线电子游戏在近期发布,在该电子游戏中,以 刻钟 为周期规划若干时长为 15 分钟 的游戏对局。这意味着,在 HH:00、HH:15、HH:30 和 HH:45 ,将会开始一个新的对局,其中 HH 用一个从 00 到 23 的整数表示。游戏中使用 24 小时制的时钟 ,所以一天中最早的时间是 00:00 ,最晚的时间是 23:59 。
给你两个字符串 startTime 和 finishTime ,均符合 "HH:MM" 格式,分别表示你 进入 和 退出 游戏的确切时间,请计算在整个游戏会话期间,你完成的 完整对局的对局数 。
- 例如,如果
startTime = "05:20"且finishTime = "05:59",这意味着你仅仅完成从05:30到05:45这一个完整对局。而你没有完成从05:15到05:30的完整对局,因为你是在对局开始后进入的游戏;同时,你也没有完成从05:45到06:00的完整对局,因为你是在对局结束前退出的游戏。
如果 finishTime 早于 startTime ,这表示你玩了个通宵(也就是从 startTime 到午夜,再从午夜到 finishTime)。
假设你是从 startTime 进入游戏,并在 finishTime 退出游戏,请计算并返回你完成的 完整对局的对局数 。
示例 1:
输入:startTime = "12:01", finishTime = "12:44" 输出:1 解释:你完成了从 12:15 到 12:30 的一个完整对局。 你没有完成从 12:00 到 12:15 的完整对局,因为你是在对局开始后的 12:01 进入的游戏。 你没有完成从 12:30 到 12:45 的完整对局,因为你是在对局结束前的 12:44 退出的游戏。
示例 2:
输入:startTime = "20:00", finishTime = "06:00" 输出:40 解释:你完成了从 20:00 到 00:00 的 16 个完整的对局,以及从 00:00 到 06:00 的 24 个完整的对局。 16 + 24 = 40
示例 3:
输入:startTime = "00:00", finishTime = "23:59" 输出:95 解释:除最后一个小时你只完成了 3 个完整对局外,其余每个小时均完成了 4 场完整对局。
提示:
startTime和finishTime的格式为HH:MM00 <= HH <= 2300 <= MM <= 59startTime和finishTime不相等
解题思路
方法一:转换为分钟数
我们可以将输入的字符串转换为分钟数 和 ,如果 ,则说明跨越了午夜,需要将 加上一天的分钟数 。
然后我们将 向上取整到 的倍数,将 向下取整到 的倍数,最后返回 与 的差值即可,注意要取 和 中的较大值。
时间复杂度 ,空间复杂度 。
class Solution:
def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
def f(s: str) -> int:
return int(s[:2]) * 60 + int(s[3:])
a, b = f(loginTime), f(logoutTime)
if a > b:
b += 1440
a, b = (a + 14) // 15, b // 15
return max(0, b - a)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity depends on the final approach, but typically involves constant-time calculations based on simple math and comparisons. The space complexity is minimal, involving only a few integer variables for storing intermediate calculations. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Ensure the candidate handles edge cases like spanning midnight correctly.
- question_mark
Look for clear, methodical handling of time and intervals in the solution.
- question_mark
Evaluate the candidate’s understanding of time manipulations and boundary conditions.
常见陷阱
外企场景- error
Failing to account for the transition across midnight, leading to incorrect results.
- error
Not recognizing that the day can be treated as 48 hours instead of 24, which simplifies the logic.
- error
Incorrectly handling the last partial round before logoutTime if it's not fully completed.
进阶变体
外企场景- arrow_right_alt
Consider scenarios where both loginTime and logoutTime are within the same day.
- arrow_right_alt
Test for edge cases with times just before or after midnight, e.g., 23:59 and 00:00.
- arrow_right_alt
Alter the round duration to something other than 15 minutes and calculate the new number of rounds.