Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aaff2ad

Browse files
✨feat: Add 1629
1 parent 9d4edab commit aaff2ad

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
| [1576. 替换所有的问号](https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-fa1u/) | 简单 | 🤩🤩🤩🤩🤩 |
8787
| [1583. 统计不开心的朋友](https://leetcode-cn.com/problems/count-unhappy-friends/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-unhappy-friends/solution/gong-shui-san-xie-ha-xi-biao-mo-ni-ti-by-2qy0/) | 中等 | 🤩🤩🤩🤩 |
8888
| [1614. 括号的最大嵌套深度](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-pf5d/) | 简单 | 🤩🤩🤩🤩🤩 |
89+
| [1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/slowest-key/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-zjwb/) | 简单 | 🤩🤩🤩🤩🤩 |
8990
| [1646. 获取生成数组中的最大值](https://leetcode-cn.com/problems/get-maximum-in-generated-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-maximum-in-generated-array/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-sj53/) | 简单 | 🤩🤩🤩🤩 |
9091
| [1720. 解码异或后的数组](https://leetcode-cn.com/problems/decode-xored-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-array/solution/gong-shui-san-xie-li-yong-yi-huo-xing-zh-p1bi/) | 简单 | 🤩🤩🤩 |
9192
| [1736. 替换隐藏数字得到的最晚时间](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/solution/gong-shui-san-xie-ti-huan-yin-cang-shu-z-2l1h/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-zjwb/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
LeetCode 设计了一款新式键盘,正在测试其可用性。测试人员将会点击一系列键(总计 `n` 个),每次一个。
10+
11+
给你一个长度为 `n` 的字符串 `keysPressed` ,其中 `keysPressed[i]` 表示测试序列中第 i 个被按下的键。`releaseTimes` 是一个升序排列的列表,其中 `releaseTimes[i]` 表示松开第 `i` 个键的时间。字符串和数组的 下标都从 `0` 开始 。第 `0` 个键在时间为 `0` 时被按下,接下来每个键都 恰好 在前一个键松开时被按下。
12+
13+
测试人员想要找出按键 持续时间最长 的键。第 `i` 次按键的持续时间为 `releaseTimes[i] - releaseTimes[i - 1]` ,第 `0` 次按键的持续时间为 `releaseTimes[0]`
14+
15+
注意,测试期间,同一个键可以在不同时刻被多次按下,而每次的持续时间都可能不同。
16+
17+
请返回按键 持续时间最长 的键,如果有多个这样的键,则返回 按字母顺序排列最大 的那个键。
18+
19+
示例 1:
20+
```
21+
输入:releaseTimes = [9,29,49,50], keysPressed = "cbcd"
22+
23+
输出:"c"
24+
25+
解释:按键顺序和持续时间如下:
26+
按下 'c' ,持续时间 9(时间 0 按下,时间 9 松开)
27+
按下 'b' ,持续时间 29 - 9 = 20(松开上一个键的时间 9 按下,时间 29 松开)
28+
按下 'c' ,持续时间 49 - 29 = 20(松开上一个键的时间 29 按下,时间 49 松开)
29+
按下 'd' ,持续时间 50 - 49 = 1(松开上一个键的时间 49 按下,时间 50 松开)
30+
按键持续时间最长的键是 'b' 和 'c'(第二次按下时),持续时间都是 20
31+
'c' 按字母顺序排列比 'b' 大,所以答案是 'c'
32+
```
33+
示例 2:
34+
```
35+
输入:releaseTimes = [12,23,36,46,62], keysPressed = "spuda"
36+
37+
输出:"a"
38+
39+
解释:按键顺序和持续时间如下:
40+
按下 's' ,持续时间 12
41+
按下 'p' ,持续时间 23 - 12 = 11
42+
按下 'u' ,持续时间 36 - 23 = 13
43+
按下 'd' ,持续时间 46 - 36 = 10
44+
按下 'a' ,持续时间 62 - 46 = 16
45+
按键持续时间最长的键是 'a' ,持续时间 16
46+
```
47+
48+
提示:
49+
* `releaseTimes.length == n`
50+
* `keysPressed.length == n`
51+
* `2 <= n <= 1000`
52+
* `1 <= releaseTimes[i] <= 10^9`
53+
* `releaseTimes[i] < releaseTimes[i+1]`
54+
* `keysPressed` 仅由小写英文字母组成
55+
56+
57+
---
58+
59+
### 模拟
60+
61+
为了方便,我们用 $rt$ 来代指 $releaseTimes,ドル用 $kp$ 来代指 $keysPressed$。
62+
63+
根据题意,从先往后处理每个 $kp[i],ドル计算每次的持续时间(当前结束时间与上次时间的差值)$rt[i] - rt[i - 1],ドル遍历过程中维护最大持续时间和对应的字符下标。
64+
65+
代码:
66+
```Java
67+
class Solution {
68+
public char slowestKey(int[] rt, String kp) {
69+
int n = rt.length, idx = 0, max = rt[0];
70+
for (int i = 1; i < n; i++) {
71+
int cur = rt[i] - rt[i - 1];
72+
if (cur > max) {
73+
idx = i; max = cur;
74+
} else if (cur == max && kp.charAt(i) > kp.charAt(idx)) {
75+
idx = i;
76+
}
77+
}
78+
return kp.charAt(idx);
79+
}
80+
}
81+
```
82+
* 时间复杂度:$O(n)$
83+
* 空间复杂度:$O(1)$
84+
85+
---
86+
87+
### 最后
88+
89+
这是我们「刷穿 LeetCode」系列文章的第 `No.1629` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
90+
91+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
92+
93+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
94+
95+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
96+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /