Class Solution

java.lang.Object
g1601_1700.s1629_slowest_key.Solution

public class Solution extends Object
1629 - Slowest Key.<p>Easy</p> <p>A newly designed keypad was tested, where a tester pressed a sequence of <code>n</code> keys, one at a time.</p> <p>You are given a string <code>keysPressed</code> of length <code>n</code>, where <code>keysPressed[i]</code> was the <code>i<sup>th</sup></code> key pressed in the testing sequence, and a sorted list <code>releaseTimes</code>, where <code>releaseTimes[i]</code> was the time the <code>i<sup>th</sup></code> key was released. Both arrays are <strong>0-indexed</strong>. The <code>0<sup>th</sup></code> key was pressed at the time <code>0</code>, and every subsequent key was pressed at the <strong>exact</strong> time the previous key was released.</p> <p>The tester wants to know the key of the keypress that had the <strong>longest duration</strong>. The <code>i<sup>th</sup></code> keypress had a <strong>duration</strong> of <code>releaseTimes[i] - releaseTimes[i - 1]</code>, and the <code>0<sup>th</sup></code> keypress had a duration of <code>releaseTimes[0]</code>.</p> <p>Note that the same key could have been pressed multiple times during the test, and these multiple presses of the same key <strong>may not</strong> have had the same <strong>duration</strong>.</p> <p><em>Return the key of the keypress that had the <strong>longest duration</strong>. If there are multiple such keypresses, return the lexicographically largest key of the keypresses.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> releaseTimes = [9,29,49,50], keysPressed = &ldquo;cbcd&rdquo;</p> <p><strong>Output:</strong> &ldquo;c&rdquo;</p> <p><strong>Explanation:</strong> The keypresses were as follows:</p> <p>Keypress for &lsquo;c&rsquo; had a duration of 9 (pressed at time 0 and released at time 9).</p> <p>Keypress for &lsquo;b&rsquo; had a duration of 29 - 9 = 20 (pressed at time 9 right after the release of the previous character and released at time 29).</p> <p>Keypress for &lsquo;c&rsquo; had a duration of 49 - 29 = 20 (pressed at time 29 right after the release of the previous character and released at time 49).</p> <p>Keypress for &lsquo;d&rsquo; had a duration of 50 - 49 = 1 (pressed at time 49 right after the release of the previous character and released at time 50).</p> <p>The longest of these was the keypress for &lsquo;b&rsquo; and the second keypress for &lsquo;c&rsquo;, both with duration 20. &lsquo;c&rsquo; is lexicographically larger than &lsquo;b&rsquo;, so the answer is &lsquo;c&rsquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> releaseTimes = [12,23,36,46,62], keysPressed = &ldquo;spuda&rdquo;</p> <p><strong>Output:</strong> &ldquo;a&rdquo;</p> <p><strong>Explanation:</strong></p> <p>The keypresses were as follows: Keypress for &lsquo;s&rsquo; had a duration of 12.</p> <p>Keypress for &lsquo;p&rsquo; had a duration of 23 - 12 = 11.</p> <p>Keypress for &lsquo;u&rsquo; had a duration of 36 - 23 = 13.</p> <p>Keypress for &lsquo;d&rsquo; had a duration of 46 - 36 = 10.</p> <p>Keypress for &lsquo;a&rsquo; had a duration of 62 - 46 = 16.</p> <p>The longest of these was the keypress for &lsquo;a&rsquo; with duration 16.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>releaseTimes.length == n</code></li> <li><code>keysPressed.length == n</code></li> <li><code>2 <= n <= 1000</code></li> <li><code>1 <= releaseTimes[i] <= 10<sup>9</sup></code></li> <li><code>releaseTimes[i] < releaseTimes[i+1]</code></li> <li><code>keysPressed</code> contains only lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • slowestKey

      public char slowestKey(int[] releaseTimes, String keysPressed)