The problem Slowest Key Leetcode Solution provides us with a sequence of keys that have been pressed. We are also given an array or vector of times these keys have been released. The sequence of keys is given in the form of a string. So, the problem asked us to find the slowest key, which takes the longest time to act. The time taken by the key to act or type is the difference of the release times for the current key and the previous key that has been pressed. In case we come across two or more keys that take the same time. So we return the key that is lexicographically largest. Before diving deep into the solution, let us first take a look at a few examples.
releaseTimes = [9,29,49,50], keysPressed = "cbcd"
"c"
Explanation: The time taken by each of the keys is 9, 20, 20, 1. These release times are for the keys c, b, c, and d respectively. Since the release times for keys b and c are the same. We return the lexicographically largest key that is c.
Table of Contents
Approach for Slowest Key Leetcode Solution
The problem Slowest Key Leetcode Solution has already been described above. In brief, the problem has asked us to find the key that takes the longest type to get executed. If in case, two or more keys take the same amount of time to execute. Then we are required to return the lexicographically largest key. To solve the problem, we simply traverse over the keys pressed and evaluate the release time for each of the keys. For the first key it is equal to releaseTime[0], and then it is releaseTime[i] – releaseTime[i-1]. So we keep two variables, one stores the answer, and the other stores the time it takes to execute that key.
We traverse over the keys, evaluating the release time for each of them. We update the answer when we find a key that either takes a longer time or is lexicographically larger than the current answer and also takes the same time to get executed. In the end, the answer is returned to the calling function.
Code for Slowest Key Leetcode Solution
C++ code
#include <bits/stdc++.h> using namespace std; char slowestKey(vector<int>& releaseTimes, string keysPressed) { int time = releaseTimes[0]; char ans = keysPressed[0]; for(int i=1;i<keysPressed.length();i++){ int cur_time = releaseTimes[i] - releaseTimes[i-1]; if(cur_time >= time){ if(cur_time > time) ans = keysPressed[i], time = cur_time; else ans = max(ans, keysPressed[i]); } } return ans; } int main(){ vector<int> releaseTimes = {9, 29, 49, 50}; string keysPressed = "cbcd"; cout<<slowestKey(releaseTimes, keysPressed); }
c
Java code
import java.util.*; import java.lang.*; import java.io.*; class Main { public static char slowestKey(int[] releaseTimes, String keysPressed) { int time = releaseTimes[0]; char ans = keysPressed.charAt(0); for(int i=1;i<keysPressed.length();i++){ int cur_time = releaseTimes[i] - releaseTimes[i-1]; if(cur_time >= time){ if(cur_time > time){ ans = keysPressed.charAt(i); time = cur_time; } else ans = ans > keysPressed.charAt(i) ? ans : keysPressed.charAt(i); } } return ans; } public static void main (String[] args) throws java.lang.Exception { int[] releaseTimes = {9, 29, 49, 50}; String keysPressed = "cbcd"; System.out.print(slowestKey(releaseTimes, keysPressed)); } }
c
Complexity Analysis
Time Complexity
O(N), because traversed over all of the given keys.
Space Complexity
O(1), because we used only two extra variables to solve the problem.