当前位置:网站首页>Leetcode simple question: the key with the longest key duration
Leetcode simple question: the key with the longest key duration
2022-07-03 04:31:00 【·Starry Sea】
subject
LeetCode Designed a new keyboard , Testing its availability . The tester will click a series of keys ( A total of n individual ), One at a time .
Give you a length of n String keysPressed , among keysPressed[i] Represents the... In the test sequence i A pressed key .releaseTimes It's a list in ascending order , among releaseTimes[i] It means that the... Is released i Key time . String and array Subscripts are all from 0 Start . The first 0 Keys at time 0 When pressed , Next, each key just Pressed when the previous key is released .
The tester wants to find the key The longest duration Key . The first i The duration of the second key is releaseTimes[i] - releaseTimes[i - 1] , The first 0 The duration of the second key is releaseTimes[0] .
Be careful , Testing period , The same key can be pressed many times at different times , And the duration of each time may be different .
Please return to the single key The longest duration Key , If there are multiple such keys , Then return to In alphabetical order, the largest The key of .
Example 1:
Input :releaseTimes = [9,29,49,50], keysPressed = “cbcd”
Output :“c”
explain : The key sequence and duration are as follows :
Press down ‘c’ , The duration of the 9( Time 0 Press down , Time 9 Release )
Press down ‘b’ , The duration of the 29 - 9 = 20( Time to release the last key 9 Press down , Time 29 Release )
Press down ‘c’ , The duration of the 49 - 29 = 20( Time to release the last key 29 Press down , Time 49 Release )
Press down ‘d’ , The duration of the 50 - 49 = 1( Time to release the last key 49 Press down , Time 50 Release )
The key with the longest key duration is ‘b’ and ‘c’( When pressed the second time ), The duration is 20
‘c’ Arrange in alphabetical order ‘b’ Big , So the answer is ‘c’
Example 2:
Input :releaseTimes = [12,23,36,46,62], keysPressed = “spuda”
Output :“a”
explain : The key sequence and duration are as follows :
Press down ‘s’ , The duration of the 12
Press down ‘p’ , The duration of the 23 - 12 = 11
Press down ‘u’ , The duration of the 36 - 23 = 13
Press down ‘d’ , The duration of the 46 - 36 = 10
Press down ‘a’ , The duration of the 62 - 46 = 16
The key with the longest key duration is ‘a’ , The duration of the 16
Tips :
releaseTimes.length == n
keysPressed.length == n
2 <= n <= 1000
1 <= releaseTimes[i] <= 10^9
releaseTimes[i] < releaseTimes[i+1]
keysPressed It's only made up of lowercase letters
source : Power button (LeetCode)
Their thinking
Just traverse the array and string at the same time , Calculate the current character key often , Then update the maximum value according to the rules .
class Solution:
def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:
releaseTimes.insert(0,0) # For the convenience of calculating the difference, add 0
MAX=(0,'Z') # Maximum
for i in range(1,len(releaseTimes)): # Update the maximum value according to the rule
if releaseTimes[i]-releaseTimes[i-1]>MAX[0]:
MAX=(releaseTimes[i]-releaseTimes[i-1],keysPressed[i-1])
elif releaseTimes[i]-releaseTimes[i-1]==MAX[0] and keysPressed[i-1]>MAX[1]:
MAX=(MAX[0],keysPressed[i-1])
return MAX[1]

边栏推荐
- C language series - Section 3 - functions
- [set theory] set concept and relationship (set family | set family examples | multiple sets)
- How to retrieve the password for opening word files
- Joint set search: merge intervals and ask whether two numbers are in the same set
- 2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
- Kubernetes source code analysis (I)
- Two points -leetcode-540 A single element in an ordered array
- Redraw and reflow
- 因子选股-打分模型
- Web - Information Collection
猜你喜欢

Contents of welder (primary) examination and welder (primary) examination in 2022

Integration of Android high-frequency interview questions (including reference answers)

Database management tool, querious direct download
![[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN](/img/7e/50fa6f65b5a4f0bb60909f57daff56.png)
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN

使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭

Solve BP Chinese garbled code

使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
![[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius](/img/01/c118725f74e39742df021b5dbcc33b.jpg)
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius

Two drawing interfaces - 1 Matlab style interface

When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully
随机推荐
Integration of Android high-frequency interview questions (including reference answers)
How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
Factor stock selection scoring model
2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
How to retrieve the password for opening word files
How to use kotlin to improve productivity: kotlin tips
Xrandr modifier la résolution et le taux de rafraîchissement
重绘和回流
arthas watch 抓取入参的某个字段/属性
会员积分商城系统的功能介绍
Youdao cloud notes
IPhone x forgot the boot password
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg
AWS VPC
Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
解决bp中文乱码
FISCO bcos zero knowledge proof Fiat Shamir instance source code