Leetcode 1629. Slowest Key

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Slowest Key

2. Solution

解析:Version 1,遍历每个字符,计算其时间间隔,如果比之前的大,则更新时间间隔及字符,如果相等,则比较字符大小,判断是否更新字符。

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:
n = len(keysPressed)
key = keysPressed[0]
time = releaseTimes[0]
for i in range(1, n):
temp = releaseTimes[i] - releaseTimes[i-1]
if temp > time:
key = keysPressed[i]
time = temp
elif temp == time and keysPressed[i] > key:
key = keysPressed[i]
return key

Reference

  1. https://leetcode.com/problems/slowest-key/
如果有收获,可以请我喝杯咖啡!