Amazon Online Assessment (OA) - Slowest Key

Practice here: https://leetcode.com/problems/slowest-key/

Solution and Explanation

This question asks for the key with largest duration releaseTimes[i] - releaseTimes[i - 1]. We can simple loop through each release time and calculate its difference to the previous time. If multiple characters have the same duration, we want to use the lexicographically largest.

1class Solution:
2  def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:
3      slowest_key = 'a'
4      longest_duration = 0
5      n = len(keysPressed)
6
7      for i in range(n):
8          pressedTime = releaseTimes[i - 1] if i > 0 else 0
9          duration = releaseTimes[i] - pressedTime
10          if duration == longest_duration:
11              slowest_key = max(slowest_key, keysPressed[i])
12          elif duration > longest_duration:
13              slowest_key = keysPressed[i]
14              longest_duration = duration
15
16      return slowest_key
17