{"id":7549,"date":"2020-10-24T21:22:07","date_gmt":"2020-10-25T04:22:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7549"},"modified":"2020-10-24T21:22:38","modified_gmt":"2020-10-25T04:22:38","slug":"leetcode-1629-slowest-key","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1629-slowest-key\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1629. Slowest Key"},"content":{"rendered":"\n<p>A newly designed keypad was tested, where a tester pressed a sequence of&nbsp;<code>n<\/code>&nbsp;keys, one at a time.<\/p>\n\n\n\n<p>You are given a string&nbsp;<code>keysPressed<\/code>&nbsp;of length&nbsp;<code>n<\/code>, where&nbsp;<code>keysPressed[i]<\/code>&nbsp;was the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;key pressed in the testing sequence, and a sorted list&nbsp;<code>releaseTimes<\/code>, where&nbsp;<code>releaseTimes[i]<\/code>&nbsp;was the time the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;key was released. Both arrays are&nbsp;<strong>0-indexed<\/strong>. The&nbsp;<code>0<sup>th<\/sup><\/code>&nbsp;key was pressed at the time&nbsp;<code>0<\/code>,&nbsp;and every subsequent key was pressed at the&nbsp;<strong>exact<\/strong>&nbsp;time the previous key was released.<\/p>\n\n\n\n<p>The tester wants to know the key of the keypress that had the&nbsp;<strong>longest duration<\/strong>. The&nbsp;<code>i<sup>th<\/sup><\/code>keypress had a&nbsp;<strong>duration<\/strong>&nbsp;of&nbsp;<code>releaseTimes[i] - releaseTimes[i - 1]<\/code>, and the&nbsp;<code>0<sup>th<\/sup><\/code>&nbsp;keypress had a duration of&nbsp;<code>releaseTimes[0]<\/code>.<\/p>\n\n\n\n<p>Note that the same key could have been pressed multiple times during the test, and these multiple presses of the same key&nbsp;<strong>may not<\/strong>&nbsp;have had the same&nbsp;<strong>duration<\/strong>.<\/p>\n\n\n\n<p><em>Return the key of the keypress that had the&nbsp;<strong>longest duration<\/strong>. If there are multiple such keypresses, return the lexicographically largest key of the keypresses.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> releaseTimes = [9,29,49,50], keysPressed = \"cbcd\"\n<strong>Output:<\/strong> \"c\"\n<strong>Explanation:<\/strong> The keypresses were as follows:\nKeypress for 'c' had a duration of 9 (pressed at time 0 and released at time 9).\nKeypress for 'b' had a duration of 29 - 9 = 20 (pressed at time 9 right after the release of the previous character and released at time 29).\nKeypress for 'c' had a duration of 49 - 29 = 20 (pressed at time 29 right after the release of the previous character and released at time 49).\nKeypress for 'd' had a duration of 50 - 49 = 1 (pressed at time 49 right after the release of the previous character and released at time 50).\nThe longest of these was the keypress for 'b' and the second keypress for 'c', both with duration 20.\n'c' is lexicographically larger than 'b', so the answer is 'c'.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> releaseTimes = [12,23,36,46,62], keysPressed = \"spuda\"\n<strong>Output:<\/strong> \"a\"\n<strong>Explanation:<\/strong> The keypresses were as follows:\nKeypress for 's' had a duration of 12.\nKeypress for 'p' had a duration of 23 - 12 = 11.\nKeypress for 'u' had a duration of 36 - 23 = 13.\nKeypress for 'd' had a duration of 46 - 36 = 10.\nKeypress for 'a' had a duration of 62 - 46 = 16.\nThe longest of these was the keypress for 'a' with duration 16.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>releaseTimes.length == n<\/code><\/li><li><code>keysPressed.length == n<\/code><\/li><li><code>2 &lt;= n &lt;= 1000<\/code><\/li><li><code>0 &lt;= releaseTimes[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>releaseTimes[i] &lt; releaseTimes[i+1]<\/code><\/li><li><code>keysPressed<\/code>&nbsp;contains only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Straightforward<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\nclass Solution {\npublic:\n  char slowestKey(vector<int>& releaseTimes, string keysPressed) {\n    int l = releaseTimes[0];\n    char ans = keysPressed[0];\n    \n    for (int i = 1; i < releaseTimes.size(); ++i) {\n      int t = releaseTimes[i] - releaseTimes[i - 1];\n      if (t > l) { \n        ans = keysPressed[i]; \n        l = t;\n      } else if (t == l) {\n        ans = max(ans, keysPressed[i]);      \n      }\n    }\n    return ans;\n  }\n};\n<pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A newly designed keypad was tested, where a tester pressed a sequence of&nbsp;n&nbsp;keys, one at a time. You are given a string&nbsp;keysPressed&nbsp;of length&nbsp;n, where&nbsp;keysPressed[i]&nbsp;was the&nbsp;ith&nbsp;key&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,376],"class_list":["post-7549","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-on","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7549"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7549\/revisions"}],"predecessor-version":[{"id":7551,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7549\/revisions\/7551"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}