Skip to content

Commit 620b19c

Browse files
author
robot
committed
feat: $1904
1 parent 268ab5e commit 620b19c

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
395395
- [1737. 满足三条件之一需改变的最少字符数](./problems/1737.change-minimum-characters-to-satisfy-one-of-three-conditions.md)
396396
- [1834. 单线程 CPU](./problems/1834.single-threaded-cpu.md)
397397
- [1899. 合并若干三元组以形成目标三元组](./problems/1899.merge-triplets-to-form-target-triplet.md) 👍
398+
- [1904. 你完成的完整对局数](./problems/1904.the-number-of-full-rounds-you-have-played.md)
398399
- [1906. 查询差绝对值的最小值](./problems/1906.minimum-absolute-difference-queries.md)
399400

400401
### 困难难度题目合集

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
- [1737. 满足三条件之一需改变的最少字符数](./problems/1737.change-minimum-characters-to-satisfy-one-of-three-conditions.md) 👍
254254
- [1834. 单线程 CPU](./problems/1834.single-threaded-cpu.md) 🆕
255255
- [1899. 合并若干三元组以形成目标三元组](./problems/1899.merge-triplets-to-form-target-triplet.md) 👍
256+
- [1904. 你完成的完整对局数](./problems/1904.the-number-of-full-rounds-you-have-played.md)
256257
- [1906. 查询差绝对值的最小值](./problems/1906.minimum-absolute-difference-queries.md)
257258

258259
- [第六章 - 高频考题(困难)](collections/hard.md)
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
## 题目地址(1904. 你完成的完整对局数)
2+
3+
https://leetcode-cn.com/problems/the-number-of-full-rounds-you-have-played/
4+
5+
## 题目描述
6+
7+
```
8+
一款新的在线电子游戏在近期发布,在该电子游戏中,以 刻钟 为周期规划若干时长为 15 分钟 的游戏对局。这意味着,在 HH:00、HH:15、HH:30 和 HH:45 ,将会开始一个新的对局,其中 HH 用一个从 00 到 23 的整数表示。游戏中使用 24 小时制的时钟 ,所以一天中最早的时间是 00:00 ,最晚的时间是 23:59 。
9+
10+
给你两个字符串 startTime 和 finishTime ,均符合 "HH:MM" 格式,分别表示你 进入 和 退出 游戏的确切时间,请计算在整个游戏会话期间,你完成的 完整对局的对局数 。
11+
12+
例如,如果 startTime = "05:20" 且 finishTime = "05:59" ,这意味着你仅仅完成从 05:30 到 05:45 这一个完整对局。而你没有完成从 05:15 到 05:30 的完整对局,因为你是在对局开始后进入的游戏;同时,你也没有完成从 05:45 到 06:00 的完整对局,因为你是在对局结束前退出的游戏。
13+
14+
如果 finishTime 早于 startTime ,这表示你玩了个通宵(也就是从 startTime 到午夜,再从午夜到 finishTime)。
15+
16+
假设你是从 startTime 进入游戏,并在 finishTime 退出游戏,请计算并返回你完成的 完整对局的对局数 。
17+
18+
 
19+
20+
示例 1:
21+
22+
输入:startTime = "12:01", finishTime = "12:44"
23+
输出:1
24+
解释:你完成了从 12:15 到 12:30 的一个完整对局。
25+
你没有完成从 12:00 到 12:15 的完整对局,因为你是在对局开始后的 12:01 进入的游戏。
26+
你没有完成从 12:30 到 12:45 的完整对局,因为你是在对局结束前的 12:44 退出的游戏。
27+
28+
29+
示例 2:
30+
31+
输入:startTime = "20:00", finishTime = "06:00"
32+
输出:40
33+
解释:你完成了从 20:00 到 00:00 的 16 个完整的对局,以及从 00:00 到 06:00 的 24 个完整的对局。
34+
16 + 24 = 40
35+
36+
37+
示例 3:
38+
39+
输入:startTime = "00:00", finishTime = "23:59"
40+
输出:95
41+
解释:除最后一个小时你只完成了 3 个完整对局外,其余每个小时均完成了 4 场完整对局。
42+
43+
44+
 
45+
46+
提示:
47+
48+
startTime 和 finishTime 的格式为 HH:MM
49+
00 <= HH <= 23
50+
00 <= MM <= 59
51+
startTime 和 finishTime 不相等
52+
```
53+
54+
## 前置知识
55+
56+
- 暂无
57+
58+
## 公司
59+
60+
- 暂无
61+
62+
## 思路
63+
64+
我们可以将开始时间和结束时间先进行一次规范化处理,这样可以减少判断。
65+
66+
具体来说,我们可以对开始时间的分数进行如下处理:
67+
68+
- 如果开始时间的分数在 (0,15) 之间,那么可以等价于在 15 分开始,因此可以将开始时间直接置为 15 而不会影响答案。
69+
- 类似地开始时间在 (15,30)可以置为 30。
70+
- ...
71+
72+
需要注意的是对于 (45, 60) 置为 0 的过程,需要将小时进位。
73+
74+
结束时间也是类似的,不再赘述,大家看代码即可。
75+
76+
接下来,我们计算结束时间和开始时间之间的分钟差 span,计算 span 拥有多少完成的 15 min 即可,也就是说可以用 span 整除 15 即可。
77+
78+
## 关键点
79+
80+
- 将开始时间和结束时间**规范到**标准时间
81+
82+
## 代码
83+
84+
- 语言支持:Python3
85+
86+
Python3 Code:
87+
88+
```python
89+
90+
class Solution:
91+
def numberOfRounds(self, startTime: str, finishTime: str) -> int:
92+
sh, sm = map(int, startTime.split(":"))
93+
eh, em = map(int, finishTime.split(":"))
94+
if 0 < sm < 15:
95+
sm = 15
96+
elif 15 < sm < 30:
97+
sm = 30
98+
elif 30 < sm < 45:
99+
sm = 45
100+
elif 45 < sm < 60:
101+
sm = 0
102+
sh += 1
103+
if 0 < em < 15:
104+
em = 0
105+
elif 15 < em < 30:
106+
em = 15
107+
elif 30 < em < 45:
108+
em = 30
109+
elif 45 < em < 60:
110+
em = 45
111+
st = sh * 60 + sm
112+
et = eh * 60 + em
113+
if st > et:
114+
et += 24 * 60
115+
return (et - st) // 15
116+
117+
```
118+
119+
**复杂度分析**
120+
121+
令 n 为数组长度。
122+
123+
- 时间复杂度:$O(n)$
124+
- 空间复杂度:$O(n)$
125+
126+
> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。
127+
128+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
129+
130+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
131+
132+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
133+
134+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
 (0)