{"id":9344,"date":"2021-12-31T14:41:17","date_gmt":"2021-12-31T22:41:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9344"},"modified":"2021-12-31T14:52:43","modified_gmt":"2021-12-31T22:52:43","slug":"leetcode-1974-minimum-time-to-type-word-using-special-typewriter","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1974-minimum-time-to-type-word-using-special-typewriter\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1974. Minimum Time to Type Word Using Special Typewriter"},"content":{"rendered":"\n<p>There is a special typewriter with lowercase English letters&nbsp;<code>'a'<\/code>&nbsp;to&nbsp;<code>'z'<\/code>&nbsp;arranged in a&nbsp;<strong>circle<\/strong>&nbsp;with a&nbsp;<strong>pointer<\/strong>. A character can&nbsp;<strong>only<\/strong>&nbsp;be typed if the pointer is pointing to that character. The pointer is&nbsp;<strong>initially<\/strong>&nbsp;pointing to the character&nbsp;<code>'a'<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/07\/31\/chart.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Each second, you may perform one of the following operations:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Move the pointer one character&nbsp;<strong>counterclockwise<\/strong>&nbsp;or&nbsp;<strong>clockwise<\/strong>.<\/li><li>Type the character the pointer is&nbsp;<strong>currently<\/strong>&nbsp;on.<\/li><\/ul>\n\n\n\n<p>Given a string&nbsp;<code>word<\/code>, return the<strong>&nbsp;minimum<\/strong>&nbsp;number of seconds to type out the characters in&nbsp;<code>word<\/code>.<\/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> word = \"abc\"\n<strong>Output:<\/strong> 5\n<strong>Explanation: \n<\/strong>The characters are printed as follows:\n- Type the character 'a' in 1 second since the pointer is initially on 'a'.\n- Move the pointer clockwise to 'b' in 1 second.\n- Type the character 'b' in 1 second.\n- Move the pointer clockwise to 'c' in 1 second.\n- Type the character 'c' in 1 second.\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> word = \"bza\"\n<strong>Output:<\/strong> 7\n<strong>Explanation:\n<\/strong>The characters are printed as follows:\n- Move the pointer clockwise to 'b' in 1 second.\n- Type the character 'b' in 1 second.\n- Move the pointer counterclockwise to 'z' in 2 seconds.\n- Type the character 'z' in 1 second.\n- Move the pointer clockwise to 'a' in 1 second.\n- Type the character 'a' in 1 second.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> word = \"zjpc\"\n<strong>Output:<\/strong> 34\n<strong>Explanation:<\/strong>\nThe characters are printed as follows:\n- Move the pointer counterclockwise to 'z' in 1 second.\n- Type the character 'z' in 1 second.\n- Move the pointer clockwise to 'j' in 10 seconds.\n- Type the character 'j' in 1 second.\n- Move the pointer clockwise to 'p' in 6 seconds.\n- Type the character 'p' in 1 second.\n- Move the pointer counterclockwise to 'c' in 13 seconds.\n- Type the character 'c' in 1 second.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= word.length &lt;= 100<\/code><\/li><li><code>word<\/code>&nbsp;consists of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Clockwise or Counter-clockwise?<\/strong><\/h2>\n\n\n\n<p>For each pair of (prev, curr), choose the shortest distance.<br>One is abs(p &#8211; c), another is 26 &#8211; abs(p &#8211; c).<br>Don&#8217;t forget to add 1 for typing itself.<\/p>\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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minTimeToType(string word) {\n    int ans = 0;\n    char p = 'a';\n    for (char c : word) {\n      ans += 1 + min(abs(c - p), 26 - abs(c - p));\n      p = c;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a special typewriter with lowercase English letters&nbsp;&#8216;a&#8217;&nbsp;to&nbsp;&#8216;z&#8217;&nbsp;arranged in a&nbsp;circle&nbsp;with a&nbsp;pointer. A character can&nbsp;only&nbsp;be typed if the pointer is pointing to that character. The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[575,222,31],"class_list":["post-9344","post","type-post","status-publish","format-standard","hentry","category-math","tag-circle","tag-easy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9344","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=9344"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9344\/revisions"}],"predecessor-version":[{"id":9348,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9344\/revisions\/9348"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}