{"id":9617,"date":"2022-04-02T21:46:31","date_gmt":"2022-04-03T04:46:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9617"},"modified":"2022-04-02T21:47:15","modified_gmt":"2022-04-03T04:47:15","slug":"leetcode-2224-minimum-number-of-operations-to-convert-time","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2224-minimum-number-of-operations-to-convert-time\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2224. Minimum Number of Operations to Convert Time"},"content":{"rendered":"\n<p>You are given two strings&nbsp;<code>current<\/code>&nbsp;and&nbsp;<code>correct<\/code>&nbsp;representing two&nbsp;<strong>24-hour times<\/strong>.<\/p>\n\n\n\n<p>24-hour times are formatted as&nbsp;<code>\"HH:MM\"<\/code>, where&nbsp;<code>HH<\/code>&nbsp;is between&nbsp;<code>00<\/code>&nbsp;and&nbsp;<code>23<\/code>, and&nbsp;<code>MM<\/code>&nbsp;is between&nbsp;<code>00<\/code>&nbsp;and&nbsp;<code>59<\/code>. The earliest 24-hour time is&nbsp;<code>00:00<\/code>, and the latest is&nbsp;<code>23:59<\/code>.<\/p>\n\n\n\n<p>In one operation you can increase the time&nbsp;<code>current<\/code>&nbsp;by&nbsp;<code>1<\/code>,&nbsp;<code>5<\/code>,&nbsp;<code>15<\/code>, or&nbsp;<code>60<\/code>&nbsp;minutes. You can perform this operation&nbsp;<strong>any<\/strong>&nbsp;number of times.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum number of operations<\/strong>&nbsp;needed to convert&nbsp;<\/em><code>current<\/code><em>&nbsp;to&nbsp;<\/em><code>correct<\/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> current = \"02:30\", correct = \"04:35\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:\n<\/strong>We can convert current to correct in 3 operations as follows:\n- Add 60 minutes to current. current becomes \"03:30\".\n- Add 60 minutes to current. current becomes \"04:30\".\n- Add 5 minutes to current. current becomes \"04:35\".\nIt can be proven that it is not possible to convert current to correct in fewer than 3 operations.<\/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> current = \"11:00\", correct = \"11:01\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> We only have to add one minute to current, so the minimum number of operations needed is 1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>current<\/code>&nbsp;and&nbsp;<code>correct<\/code>&nbsp;are in the format&nbsp;<code>\"HH:MM\"<\/code><\/li><li><code>current &lt;= correct<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Start with 60, then 15, 5 and finally increase 1 minute a time.<\/p>\n\n\n\n<p>Time complexity: O(1)<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 convertTime(string current, string correct) {\n    auto getMinute = [](string_view t) {\n      return (t[0] - '0') * 10 * 60 \n             + (t[1] - '0') * 60 \n             + (t[3] - '0') * 10 \n             + (t[4] - '0');\n    };\n    \n    int t1 = getMinute(current);\n    int t2 = getMinute(correct);\n    int ans = 0;    \n    for (int d : {60, 15, 5, 1})\n      while (t2 - t1 >= d) {\n        ++ans;\n        t1 += d;\n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings&nbsp;current&nbsp;and&nbsp;correct&nbsp;representing two&nbsp;24-hour times. 24-hour times are formatted as&nbsp;&#8220;HH:MM&#8221;, where&nbsp;HH&nbsp;is between&nbsp;00&nbsp;and&nbsp;23, and&nbsp;MM&nbsp;is between&nbsp;00&nbsp;and&nbsp;59. The earliest 24-hour time is&nbsp;00:00, and the latest is&nbsp;23:59.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[222,88,4,107],"class_list":["post-9617","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-easy","tag-greedy","tag-string","tag-time","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9617","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=9617"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9617\/revisions"}],"predecessor-version":[{"id":9619,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9617\/revisions\/9619"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}