{"id":2279,"date":"2018-03-21T21:51:03","date_gmt":"2018-03-22T04:51:03","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2279"},"modified":"2018-03-21T22:12:18","modified_gmt":"2018-03-22T05:12:18","slug":"leetcode-539-minimum-time-difference","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-539-minimum-time-difference\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 539. Minimum Time Difference"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a list of 24-hour clock time points in &#8220;Hour:Minutes&#8221; format, find the minimum\u00a0<b>minutes<\/b>\u00a0difference between any two time points in the list.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> [\"23:59\",\"00:00\"]\r\n<b>Output:<\/b> 1\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The number of time points in the given list is at least 2 and won&#8217;t exceed 20000.<\/li>\n<li>The input time is legal and ranges from 00:00 to 23:59.<\/li>\n<\/ol>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(nlog1440)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  int findMinDifference(vector&lt;string&gt;&amp; timePoints) {    \r\n    constexpr int kMins = 24 * 60;\r\n    set&lt;int&gt; seen;    \r\n    for (const string&amp; time : timePoints) {\r\n      int m = stoi(time.substr(0, 2)) * 60 + stoi(time.substr(3));\r\n      if (!seen.insert(m).second) return 0;\r\n    }\r\n    \r\n    int ans = (*seen.begin() - *seen.rbegin() + kMins) % kMins;\r\n    const int* l = nullptr;\r\n    for (const int&amp; t : seen) {\r\n      if (l) ans = min(ans, t - *l);\r\n      l = &amp;t;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a list of 24-hour clock time points in &#8220;Hour:Minutes&#8221; format, find the minimum\u00a0minutes\u00a0difference between any two time points in the list. Example 1:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[108,222,4,107],"class_list":["post-2279","post","type-post","status-publish","format-standard","hentry","category-string","tag-clock","tag-easy","tag-string","tag-time","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2279","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=2279"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2279\/revisions"}],"predecessor-version":[{"id":2285,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2279\/revisions\/2285"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}