Skip to content

1629. Slowest Key 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  char slowestKey(vector<int>& releaseTimes, string keysPressed) {
    char ans = keysPressed[0];
    int maxDuration = releaseTimes[0];

    for (int i = 1; i < keysPressed.length(); ++i) {
      const int duration = releaseTimes[i] - releaseTimes[i - 1];
      if (duration > maxDuration ||
          (duration == maxDuration && keysPressed[i] > ans)) {
        ans = keysPressed[i];
        maxDuration = duration;
      }
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
  public char slowestKey(int[] releaseTimes, String keysPressed) {
    char ans = keysPressed.charAt(0);
    int maxDuration = releaseTimes[0];

    for (int i = 1; i < keysPressed.length(); ++i) {
      final int duration = releaseTimes[i] - releaseTimes[i - 1];
      if (duration > maxDuration || (duration == maxDuration && keysPressed.charAt(i) > ans)) {
        ans = keysPressed.charAt(i);
        maxDuration = duration;
      }
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
  def slowestKey(self, releaseTimes: list[int], keysPressed: str) -> str:
    ans = keysPressed[0]
    maxDuration = releaseTimes[0]

    for i in range(1, len(keysPressed)):
      duration = releaseTimes[i] - releaseTimes[i-1]
      if duration > maxDuration or (
              duration == maxDuration and keysPressed[i] > ans):
        ans = keysPressed[i]
        maxDuration = duration

    return ans