Skip to content

Commit 3e3e360

Browse files
committed
#1629: Slowest Key; solution & tests.
1 parent 5aa53c7 commit 3e3e360

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* 1629
3+
* Slowest Key
4+
**
5+
* A newly designed keypad was tested,
6+
* where a tester pressed a sequence of n keys,
7+
* one at a time.
8+
*
9+
* You are given a string keysPressed of length n,
10+
* where keysPressed[i] was the ith key pressed in the testing sequence,
11+
* and a sorted list releaseTimes,
12+
* where releaseTimes[i] was the time the ith key was released.
13+
* Both arrays are 0-indexed.
14+
* The 0th key was pressed at the time 0,
15+
* and every subsequent key was pressed at the exact time the previous key was released.
16+
*
17+
* The tester wants to know the key of the keypress that had the longest duration.
18+
* The ith keypress had a duration of releaseTimes[i] - releaseTimes[i - 1],
19+
* and the 0th keypress had a duration of releaseTimes[0].
20+
*
21+
* Note that the same key could have been pressed multiple times during the test,
22+
* and these multiple presses of the same key may not have had the same duration.
23+
*
24+
* Return the key of the keypress that had the longest duration.
25+
* If there are multiple such keypresses,
26+
* return the lexicographically largest key of the keypresses.
27+
*
28+
* Example 1:
29+
* Input: releaseTimes = [9,29,49,50], keysPressed = "cbcd"
30+
* Output: "c"
31+
* Explanation: The keypresses were as follows:
32+
* - Keypress for 'c' had a duration of 9 (pressed at time 0 and released at time 9).
33+
* - Keypress for 'b' had a duration of 29 - 9 = 20
34+
* (pressed at time 9 right after the release of the previous character
35+
* and released at time 29).
36+
* - Keypress for 'c' had a duration of 49 - 29 = 20
37+
* (pressed at time 29 right after the release of the previous character
38+
* and released at time 49).
39+
* - Keypress for 'd' had a duration of 50 - 49 = 1
40+
* (pressed at time 49 right after the release of the previous character
41+
* and released at time 50).
42+
* - The longest of these was the keypress for 'b' and the second keypress for 'c',
43+
* both with duration 20.
44+
* - 'c' is lexicographically larger than 'b',
45+
* so the answer is 'c'.
46+
*
47+
* Example 2:
48+
* Input: releaseTimes = [12,23,36,46,62], keysPressed = "spuda"
49+
* Output: "a"
50+
* Explanation: The keypresses were as follows:
51+
* - Keypress for 's' had a duration of 12.
52+
* - Keypress for 'p' had a duration of 23 - 12 = 11.
53+
* - Keypress for 'u' had a duration of 36 - 23 = 13.
54+
* - Keypress for 'd' had a duration of 46 - 36 = 10.
55+
* - Keypress for 'a' had a duration of 62 - 46 = 16.
56+
* - The longest of these was the keypress for 'a' with duration 16.
57+
*
58+
* Constraints:
59+
* • releaseTimes.length == n
60+
* • keysPressed.length == n
61+
* • 2 <= n <= 1000
62+
* • 1 <= releaseTimes[i] <= 10^9
63+
* • releaseTimes[i] < releaseTimes[i+1]
64+
* • keysPressed contains only lowercase English letters.
65+
*
66+
* Hint 1:
67+
* Get for each press its key and amount of time taken.
68+
*
69+
* Hint 2:
70+
* Iterate on the presses, maintaining the answer so far.
71+
*
72+
* Hint 3:
73+
* The current press will change the answer
74+
* if and only if its amount of time taken is longer than that of the previous answer,
75+
* or they are equal but the key is larger than that of the previous answer.
76+
**
77+
* https://leetcode.com/problems/slowest-key/
78+
***/
79+
80+
namespace Problems;
81+
82+
public class SlowestKeySolution
83+
{
84+
public char SlowestKey( int[] releaseTimes, string keysPressed )
85+
{
86+
char result = default;
87+
int maxDuration = 0;
88+
89+
for ( int i = 0; i < releaseTimes.Length; i++ )
90+
{
91+
int duration = releaseTimes[i] - ( i == 0 ? 0 : releaseTimes[i - 1] );
92+
93+
if ( duration > maxDuration )
94+
{
95+
result = keysPressed[i];
96+
maxDuration = duration;
97+
}
98+
else if ( duration == maxDuration )
99+
{
100+
result = keysPressed[i] > result ? keysPressed[i] : result;
101+
}
102+
}
103+
104+
return result;
105+
}
106+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using NUnit.Framework;
2+
3+
using Problems;
4+
5+
public class SlowestKeySolutionTests
6+
{
7+
[TestCase( new int[] { 9, 29, 49, 50 }, "cbcd", ExpectedResult = 'c' )]
8+
[TestCase( new int[] { 12, 23, 36, 46, 62 }, "spuda", ExpectedResult = 'a' )]
9+
public char SlowestKeyTest( int[] releaseTimes, string keysPressed ) =>
10+
new SlowestKeySolution().SlowestKey( releaseTimes, keysPressed );
11+
}

0 commit comments

Comments
 (0)