LeetCode 题解工作台

你完成的完整对局数

一款新的在线电子游戏在近期发布,在该电子游戏中,以 刻钟 为周期规划若干时长为 15 分钟 的游戏对局。这意味着,在 HH:00 、 HH:15 、 HH:30 和 HH:45 ,将会开始一个新的对局,其中 HH 用一个从 00 到 23 的整数表示。游戏中使用 24 小时制的时钟 ,所以一天中最早…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数学·string

bolt

答案摘要

我们可以将输入的字符串转换为分钟数 和 ,如果 $a > b$,则说明跨越了午夜,需要将 加上一天的分钟数 。 然后我们将 向上取整到 的倍数,将 向下取整到 的倍数,最后返回 与 的差值即可,注意要取 和 $b - a$ 中的较大值。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数学·string 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

一款新的在线电子游戏在近期发布,在该电子游戏中,以 刻钟 为周期规划若干时长为 15 分钟 的游戏对局。这意味着,在 HH:00HH:15HH:30HH:45 ,将会开始一个新的对局,其中 HH 用一个从 0023 的整数表示。游戏中使用 24 小时制的时钟 ,所以一天中最早的时间是 00:00 ,最晚的时间是 23:59

给你两个字符串 startTimefinishTime ,均符合 "HH:MM" 格式,分别表示你 进入退出 游戏的确切时间,请计算在整个游戏会话期间,你完成的 完整对局的对局数

  • 例如,如果 startTime = "05:20"finishTime = "05:59" ,这意味着你仅仅完成从 05:3005:45 这一个完整对局。而你没有完成从 05:1505:30 的完整对局,因为你是在对局开始后进入的游戏;同时,你也没有完成从 05:4506: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 场完整对局。

 

提示:

  • startTimefinishTime 的格式为 HH:MM
  • 00 <= HH <= 23
  • 00 <= MM <= 59
  • startTimefinishTime 不相等
lightbulb

解题思路

方法一:转换为分钟数

我们可以将输入的字符串转换为分钟数 aabb,如果 a>ba > b,则说明跨越了午夜,需要将 bb 加上一天的分钟数 14401440

然后我们将 aa 向上取整到 1515 的倍数,将 bb 向下取整到 1515 的倍数,最后返回 bbaa 的差值即可,注意要取 00bab - a 中的较大值。

时间复杂度 O(1)O(1),空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
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)
speed

复杂度分析

指标
时间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
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

你完成的完整对局数题解:数学·string | LeetCode #1904 中等