当前位置:网站首页>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]
边栏推荐
- Learning practice: comprehensive application of cycle and branch structure (I)
- 一名外包仔的2022年中总结
- Introduction of pointer variables in function parameters
- Kingbasees plug-in KDB of Jincang database_ exists_ expand
- Function introduction of member points mall system
- vulnhub HA: Natraj
- 2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
- [literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
- Golang -- realize file transfer
- The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
猜你喜欢
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
P35-P41 fourth_ context
2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
Contents of welder (primary) examination and welder (primary) examination in 2022
The latest activation free version of Omni toolbox
Two points -leetcode-540 A single element in an ordered array
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
redis 持久化原理
随机推荐
The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
vulnhub HA: Natraj
Crazy scientist
Employee attendance management system based on SSM
220214c language learning diary
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
Hj35 serpentine matrix
Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg
商城系统搭建完成后需要设置哪些功能
使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver
Learning practice: comprehensive application of cycle and branch structure (I)
FFMpeg example
Square root of X
有道云笔记
x Problem B
Some information about the developer environment in Chengdu
Feature_selection
因子选股-打分模型
Contents of welder (primary) examination and welder (primary) examination in 2022
智能合约安全审计公司选型分析和审计报告资源下载---国内篇