diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 9db54a05..32d75701 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -86,6 +86,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/1621-1630/1629. \346\214\211\351\224\256\346\214\201\347\273\255\346\227\266\351\227\264\346\234\200\351\225\277\347\232\204\351\224\256\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/1621-1630/1629. \346\214\211\351\224\256\346\214\201\347\273\255\346\227\266\351\227\264\346\234\200\351\225\277\347\232\204\351\224\256\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..71f5d215 --- /dev/null +++ "b/LeetCode/1621-1630/1629. \346\214\211\351\224\256\346\214\201\347\273\255\346\227\266\351\227\264\346\234\200\351\225\277\347\232\204\351\224\256\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,96 @@ +### 题目描述 + +这是 LeetCode 上的 **[1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-zjwb/)** ,难度为 **简单**。 + +Tag : 「模拟」 + + + +LeetCode 设计了一款新式键盘,正在测试其可用性。测试人员将会点击一系列键(总计 `n` 个),每次一个。 + +给你一个长度为 `n` 的字符串 `keysPressed` ,其中 `keysPressed[i]` 表示测试序列中第 i 个被按下的键。`releaseTimes` 是一个升序排列的列表,其中 `releaseTimes[i]` 表示松开第 `i` 个键的时间。字符串和数组的 下标都从 `0` 开始 。第 `0` 个键在时间为 `0` 时被按下,接下来每个键都 恰好 在前一个键松开时被按下。 + +测试人员想要找出按键 持续时间最长 的键。第 `i` 次按键的持续时间为 `releaseTimes[i] - releaseTimes[i - 1]` ,第 `0` 次按键的持续时间为 `releaseTimes[0]` 。 + +注意,测试期间,同一个键可以在不同时刻被多次按下,而每次的持续时间都可能不同。 + +请返回按键 持续时间最长 的键,如果有多个这样的键,则返回 按字母顺序排列最大 的那个键。 + +示例 1: +``` +输入:releaseTimes = [9,29,49,50], keysPressed = "cbcd" + +输出:"c" + +解释:按键顺序和持续时间如下: +按下 'c' ,持续时间 9(时间 0 按下,时间 9 松开) +按下 'b' ,持续时间 29 - 9 = 20(松开上一个键的时间 9 按下,时间 29 松开) +按下 'c' ,持续时间 49 - 29 = 20(松开上一个键的时间 29 按下,时间 49 松开) +按下 'd' ,持续时间 50 - 49 = 1(松开上一个键的时间 49 按下,时间 50 松开) +按键持续时间最长的键是 'b' 和 'c'(第二次按下时),持续时间都是 20 +'c' 按字母顺序排列比 'b' 大,所以答案是 'c' +``` +示例 2: +``` +输入:releaseTimes = [12,23,36,46,62], keysPressed = "spuda" + +输出:"a" + +解释:按键顺序和持续时间如下: +按下 's' ,持续时间 12 +按下 'p' ,持续时间 23 - 12 = 11 +按下 'u' ,持续时间 36 - 23 = 13 +按下 'd' ,持续时间 46 - 36 = 10 +按下 'a' ,持续时间 62 - 46 = 16 +按键持续时间最长的键是 'a' ,持续时间 16 +``` + +提示: +* `releaseTimes.length == n` +* `keysPressed.length == n` +* `2 <= n <= 1000` +* `1 <= releaseTimes[i] <= 10^9` +* `releaseTimes[i] < releaseTimes[i+1]` +* `keysPressed` 仅由小写英文字母组成 + + +--- + +### 模拟 + +为了方便,我们用 $rt$ 来代指 $releaseTimes$,用 $kp$ 来代指 $keysPressed$。 + +根据题意,从先往后处理每个 $kp[i]$,计算每次的持续时间(当前结束时间与上次时间的差值)$rt[i] - rt[i - 1]$,遍历过程中维护最大持续时间和对应的字符下标。 + +代码: +```Java +class Solution { + public char slowestKey(int[] rt, String kp) { + int n = rt.length, idx = 0, max = rt[0]; + for (int i = 1; i < n; i++) { + int cur = rt[i] - rt[i - 1]; + if (cur > max) { + idx = i; max = cur; + } else if (cur == max && kp.charAt(i) > kp.charAt(idx)) { + idx = i; + } + } + return kp.charAt(idx); + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1629` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +