当前位置:网站首页>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]

边栏推荐
- IPhone x forgot the boot password
- 2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
- FFMpeg example
- Redis persistence principle
- How do you use lodash linking function- How do you chain functions using lodash?
- Introduction of pointer variables in function parameters
- 跨境电商多商户系统怎么选
- [literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
- MC Layer Target
- [set theory] set identities (idempotent law | exchange law | combination law | distribution rate | De Morgan law | absorption rate | zero law | identity | exclusion law | contradiction law | complemen
猜你喜欢

vulnhub HA: Natraj

2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis

Smart contract security audit company selection analysis and audit report resources download - domestic article

vulnhub HA: Natraj
![[free completion] development of course guidance platform (source code +lunwen)](/img/14/7c1c822bda050a805fa7fc25b802a4.jpg)
[free completion] development of course guidance platform (source code +lunwen)

2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills

2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination

The latest activation free version of Omni toolbox

Youdao cloud notes

使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
随机推荐
vulnhub HA: Natraj
有道云笔记
Priv app permission exception
拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver
[Thesis Writing] how to write the overall design of JSP tourism network
2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
多板块轮动策略编写技巧----策略编写学习教材
Database management tool, querious direct download
arthas watch 抓取入参的某个字段/属性
Kingbasees plug-in KDB of Jincang database_ exists_ expand
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
MongoDB 慢查询语句优化分析策略
4 years of experience to interview test development, 10 minutes to end, ask too
JS multidimensional array to one-dimensional array
Learning practice: comprehensive application of cycle and branch structure (I)
[Chongqing Guangdong education] reference materials for design and a better life of Zhongyuan Institute of science and technology
会员积分商城系统的功能介绍
Two points -leetcode-540 A single element in an ordered array
stm32逆向入门