<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>clock Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/clock/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/clock/</link>
	<description></description>
	<lastBuildDate>Sun, 15 Aug 2021 19:22:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>clock Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/clock/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1904. The Number of Full Rounds You Have Played</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1904-the-number-of-full-rounds-you-have-played/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1904-the-number-of-full-rounds-you-have-played/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Aug 2021 19:20:36 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[clock]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[time]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8584</guid>

					<description><![CDATA[<p>A new online video game has been released, and in this video game, there are&#160;15-minute&#160;rounds scheduled every&#160;quarter-hour&#160;period. This means that at&#160;HH:00,&#160;HH:15,&#160;HH:30&#160;and&#160;HH:45, a new round starts,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1904-the-number-of-full-rounds-you-have-played/">花花酱 LeetCode 1904. The Number of Full Rounds You Have Played</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A new online video game has been released, and in this video game, there are&nbsp;<strong>15-minute</strong>&nbsp;rounds scheduled every&nbsp;<strong>quarter-hour</strong>&nbsp;period. This means that at&nbsp;<code>HH:00</code>,&nbsp;<code>HH:15</code>,&nbsp;<code>HH:30</code>&nbsp;and&nbsp;<code>HH:45</code>, a new round starts, where&nbsp;<code>HH</code>&nbsp;represents an integer number from&nbsp;<code>00</code>&nbsp;to&nbsp;<code>23</code>. A&nbsp;<strong>24-hour clock</strong>&nbsp;is used, so the earliest time in the day is&nbsp;<code>00:00</code>&nbsp;and the latest is&nbsp;<code>23:59</code>.</p>



<p>Given two strings&nbsp;<code>startTime</code>&nbsp;and&nbsp;<code>finishTime</code>&nbsp;in the format&nbsp;<code>"HH:MM"</code>&nbsp;representing the exact time you&nbsp;<strong>started</strong>&nbsp;and&nbsp;<strong>finished</strong>&nbsp;playing the game, respectively, calculate the&nbsp;<strong>number of full rounds</strong>&nbsp;that you played during your game session.</p>



<ul><li>For example, if&nbsp;<code>startTime = "05:20"</code>&nbsp;and&nbsp;<code>finishTime = "05:59"</code>&nbsp;this means you played only one full round from&nbsp;<code>05:30</code>&nbsp;to&nbsp;<code>05:45</code>. You did not play the full round from&nbsp;<code>05:15</code>&nbsp;to&nbsp;<code>05:30</code>&nbsp;because you started after the round began, and you did not play the full round from&nbsp;<code>05:45</code>&nbsp;to&nbsp;<code>06:00</code>&nbsp;because you stopped before the round ended.</li></ul>



<p>If&nbsp;<code>finishTime</code>&nbsp;is&nbsp;<strong>earlier</strong>&nbsp;than&nbsp;<code>startTime</code>, this means you have played overnight (from&nbsp;<code>startTime</code>&nbsp;to the midnight and from midnight to&nbsp;<code>finishTime</code>).</p>



<p>Return&nbsp;<em>the&nbsp;<strong>number of full rounds</strong>&nbsp;that you have played if you had started playing at&nbsp;</em><code>startTime</code><em>&nbsp;and finished at&nbsp;</em><code>finishTime</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> startTime = "12:01", finishTime = "12:44"
<strong>Output:</strong> 1
<strong>Explanation:</strong> You played one full round from 12:15 to 12:30.
You did not play the full round from 12:00 to 12:15 because you started playing at 12:01 after it began.
You did not play the full round from 12:30 to 12:45 because you stopped playing at 12:44 before it ended.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> startTime = "20:00", finishTime = "06:00"
<strong>Output:</strong> 40
<strong>Explanation:</strong> You played 16 full rounds from 20:00 to 00:00 and 24 full rounds from 00:00 to 06:00.
16 + 24 = 40.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> startTime = "00:00", finishTime = "23:59"
<strong>Output:</strong> 95
<strong>Explanation:</strong> You played 4 full rounds each hour except for the last hour where you played 3 full rounds.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>startTime</code>&nbsp;and&nbsp;<code>finishTime</code>&nbsp;are in the format&nbsp;<code>HH:MM</code>.</li><li><code>00 &lt;= HH &lt;= 23</code></li><li><code>00 &lt;= MM &lt;= 59</code></li><li><code>startTime</code>&nbsp;and&nbsp;<code>finishTime</code>&nbsp;are not equal.</li></ul>



<h2><strong>Solution: String / Simple math</strong></h2>



<p>ans = max(0, floor(end / 15) &#8211; ceil(start / 15))</p>



<p>Tips: </p>



<ol><li>Write a reusable function to parse time to minutes.</li><li>a / b for floor, (a + b &#8211; 1) / b for ceil</li></ol>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numberOfRounds(string startTime, string finishTime) {
    auto parseTime = [](string t) {
      return ((t[0] - '0') * 10 + (t[1] - '0')) * 60 + (t[3] - '0') * 10 + t[4] - '0';
    };
    int m1 = parseTime(startTime);
    int m2 = parseTime(finishTime);
    if (m2 &lt; m1) m2 += 24 * 60;
    return max(0, m2 / 15 - (m1 + 14) / 15);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1904-the-number-of-full-rounds-you-have-played/">花花酱 LeetCode 1904. The Number of Full Rounds You Have Played</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-1904-the-number-of-full-rounds-you-have-played/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1736. Latest Time by Replacing Hidden Digits</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1736-latest-time-by-replacing-hidden-digits/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1736-latest-time-by-replacing-hidden-digits/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Jan 2021 05:37:38 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[clock]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8020</guid>

					<description><![CDATA[<p>You are given a string&#160;time&#160;in the form of&#160;hh:mm, where some of the digits in the string are hidden (represented by&#160;?). The valid times are those&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1736-latest-time-by-replacing-hidden-digits/">花花酱 LeetCode 1736. Latest Time by Replacing Hidden Digits</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given a string&nbsp;<code>time</code>&nbsp;in the form of&nbsp;<code>hh:mm</code>, where some of the digits in the string are hidden (represented by&nbsp;<code>?</code>).</p>



<p>The valid times are those inclusively between&nbsp;<code>00:00</code>&nbsp;and&nbsp;<code>23:59</code>.</p>



<p>Return&nbsp;<em>the latest valid time you can get from</em>&nbsp;<code>time</code><em>&nbsp;by replacing the hidden</em>&nbsp;<em>digits</em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> time = "2?:?0"
<strong>Output:</strong> "23:50"
<strong>Explanation:</strong> The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> time = "0?:3?"
<strong>Output:</strong> "09:39"
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> time = "1?:22"
<strong>Output:</strong> "19:22"
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>time</code>&nbsp;is in the format&nbsp;<code>hh:mm</code>.</li><li>It is guaranteed that you can produce a valid time from the given string.</li></ul>



<h2><strong>Solution 1: Brute Force</strong></h2>



<p>Enumerate all possible clock in reverse order and find the first matching one.</p>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string maximumTime(string time) {
    for (int m = 60 * 24 - 1; m &gt;= 0; --m) {
      int hh = m / 60;
      int mm = m % 60;
      if (time[0] != '?' &amp;&amp; time[0] - '0' != hh / 10) continue;
      if (time[1] != '?' &amp;&amp; time[1] - '0' != hh % 10) continue;
      if (time[3] != '?' &amp;&amp; time[3] - '0' != mm / 10) continue;
      if (time[4] != '?' &amp;&amp; time[4] - '0' != mm % 10) continue;
      time[0] = (hh / 10) + '0';
      time[1] = (hh % 10) + '0';
      time[3] = (mm / 10) + '0';
      time[4] = (mm % 10) + '0';
      break;
    }
    return time;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Rules</strong></h2>



<p>Using rules, fill from left to right.</p>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string maximumTime(string time) {
    if (time[0] == '?') time[0] = time[1] &gt;= '4' &amp;&amp; time[1] &lt;= '9' ? '1' : '2';
    if (time[1] == '?') time[1] = time[0] == '2' ? '3' : '9';
    if (time[3] == '?') time[3] = '5';
    if (time[4] == '?') time[4] = '9';
    return time;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1736-latest-time-by-replacing-hidden-digits/">花花酱 LeetCode 1736. Latest Time by Replacing Hidden Digits</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-1736-latest-time-by-replacing-hidden-digits/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1344. Angle Between Hands of a Clock</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1344-angle-between-hands-of-a-clock/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1344-angle-between-hands-of-a-clock/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 14 Feb 2020 06:36:37 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[angle]]></category>
		<category><![CDATA[clock]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6309</guid>

					<description><![CDATA[<p>Given two numbers,&#160;hour&#160;and&#160;minutes. Return the smaller angle (in sexagesimal units) formed between the&#160;hour&#160;and the&#160;minute&#160;hand. Example 1: Input: hour = 12, minutes = 30 Output: 165&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1344-angle-between-hands-of-a-clock/">花花酱 LeetCode 1344. Angle Between Hands of a Clock</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given two numbers,&nbsp;<code>hour</code>&nbsp;and&nbsp;<code>minutes</code>. Return the smaller angle (in sexagesimal units) formed between the&nbsp;<code>hour</code>&nbsp;and the&nbsp;<code>minute</code>&nbsp;hand.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image is-resized"><img src="https://assets.leetcode.com/uploads/2019/12/26/sample_1_1673.png" alt="" width="184" height="181"/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> hour = 12, minutes = 30
<strong>Output:</strong> 165
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image is-resized"><img src="https://assets.leetcode.com/uploads/2019/12/26/sample_2_1673.png" alt="" width="181" height="181"/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> hour = 3, minutes = 30
<strong>Output:</strong> 75
</pre>



<p><strong>Example 3:</strong></p>



<figure class="wp-block-image is-resized"><img src="https://assets.leetcode.com/uploads/2019/12/26/sample_3_1673.png" alt="" width="181" height="181"/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> hour = 3, minutes = 15
<strong>Output:</strong> 7.5
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> hour = 4, minutes = 50
<strong>Output:</strong> 155
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> hour = 12, minutes = 0
<strong>Output:</strong> 0
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= hour &lt;= 12</code></li><li><code>0 &lt;= minutes &lt;= 59</code></li><li>Answers within&nbsp;<code>10^-5</code>&nbsp;of the actual value will be accepted as correct.</li></ul>



<p>Solution: Math</p>



<ol><li>Compute the angle of the hour hand (h  + m / 60.0) * 360 / 12 as a_h</li><li>Compute the angle of the minute hand m / 60.0 * 360 as a_m</li><li>ans = min(abs(a_h &#8211; a_m), 360 &#8211; abs(a_h &#8211; a_m))</li></ol>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  double angleClock(int hour, int minutes) {
    double a_m = minutes * 360 / 60;
    double a_h = (hour + minutes / 60.0) * 360 / 12;
    return min(abs(a_m - a_h), 360 - abs(a_m - a_h));
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1344-angle-between-hands-of-a-clock/">花花酱 LeetCode 1344. Angle Between Hands of a Clock</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-1344-angle-between-hands-of-a-clock/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 539. Minimum Time Difference</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-539-minimum-time-difference/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-539-minimum-time-difference/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 22 Mar 2018 04:51:03 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[clock]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[time]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2279</guid>

					<description><![CDATA[<p>Problem Given a list of 24-hour clock time points in &#8220;Hour:Minutes&#8221; format, find the minimum minutes difference between any two time points in the list. Example 1:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-539-minimum-time-difference/">花花酱 LeetCode 539. Minimum Time Difference</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given a list of 24-hour clock time points in &#8220;Hour:Minutes&#8221; format, find the minimum <b>minutes</b> difference between any two time points in the list.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> ["23:59","00:00"]
<b>Output:</b> 1
</pre>
<p><b>Note:</b></p>
<ol>
<li>The number of time points in the given list is at least 2 and won&#8217;t exceed 20000.</li>
<li>The input time is legal and ranges from 00:00 to 23:59.</li>
</ol>
<h1><strong>Solution</strong></h1>
<p>Time complexity: O(nlog1440)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: huahua
// Running time: 16 ms
class Solution {
public:
  int findMinDifference(vector&lt;string&gt;&amp; timePoints) {    
    constexpr int kMins = 24 * 60;
    set&lt;int&gt; seen;    
    for (const string&amp; time : timePoints) {
      int m = stoi(time.substr(0, 2)) * 60 + stoi(time.substr(3));
      if (!seen.insert(m).second) return 0;
    }
    
    int ans = (*seen.begin() - *seen.rbegin() + kMins) % kMins;
    const int* l = nullptr;
    for (const int&amp; t : seen) {
      if (l) ans = min(ans, t - *l);
      l = &amp;t;
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-539-minimum-time-difference/">花花酱 LeetCode 539. Minimum Time Difference</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-539-minimum-time-difference/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 401. Binary Watch</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-401-binary-watch/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-401-binary-watch/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 17 Mar 2018 17:31:55 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[clock]]></category>
		<category><![CDATA[combination]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2154</guid>

					<description><![CDATA[<p>Problem: A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). Each LED represents&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-401-binary-watch/">花花酱 LeetCode 401. Binary Watch</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Problem:</h1>
<p>A binary watch has 4 LEDs on the top which represent the <b>hours</b> (<b>0-11</b>), and the 6 LEDs on the bottom represent the <b>minutes</b> (<b>0-59</b>).</p>
<p>Each LED represents a zero or one, with the least significant bit on the right.</p>
<h1><img src="https://upload.wikimedia.org/wikipedia/commons/8/8b/Binary_clock_samui_moon.jpg" height="300" /></h1>
<p>For example, the above binary watch reads &#8220;3:25&#8221;.</p>
<p>Given a non-negative integer <i>n</i> which represents the number of LEDs that are currently on, return all possible times the watch could represent.</p>
<p><b>Example:</b></p>
<pre class="crayon: false">Input: n = 1
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]</pre>
<p><b>Note:</b></p>
<ul>
<li>The order of output does not matter.</li>
<li>The hour must not contain a leading zero, for example &#8220;01:00&#8221; is not valid, it should be &#8220;1:00&#8221;.</li>
<li>The minute must be consist of two digits and may contain a leading zero, for example &#8220;10:2&#8221; is not valid, it should be &#8220;10:02&#8221;.</li>
</ul>
<h1>Solution 1:</h1>
<p>Time complexity: O(11*59*n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 2 ms (beats 100%)
class Solution {
public:
  vector&lt;string&gt; readBinaryWatch(int num) {
    vector&lt;string&gt; ans;
    for (int i = 0; i &lt;= num; ++i)
      for (int h : nums(i, 12))
        for (int m : nums(num - i, 60))
          ans.push_back(to_string(h) + (m &lt; 10 ? ":0" : ":") + to_string(m));
    return ans;
  }
private:
  // Return numbers in [0,r) that has b 1s in their binary format.
  vector&lt;int&gt; nums(int b, int r) {    
    vector&lt;int&gt; ans;
    for (int n = 0; n &lt; r; ++n)
      if (__builtin_popcount(n) == b) ans.push_back(n);
    return ans;
  }  
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-401-binary-watch/">花花酱 LeetCode 401. Binary Watch</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/bit/leetcode-401-binary-watch/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 681. Next Closest Time</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-681-next-closest-time/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-681-next-closest-time/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 25 Sep 2017 04:01:14 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[clock]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[time]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=403</guid>

					<description><![CDATA[<p>Problem: https://leetcode.com/problems/next-closest-time/description/ no limit on how many times a digit can be reused. You may assume the given input string is always valid. For example,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-681-next-closest-time/">花花酱 LeetCode 681. Next Closest Time</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/IAet94C1FCc?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem:</strong></h1>
<p><a href="https://leetcode.com/problems/next-closest-time/description/">https://leetcode.com/problems/next-closest-time/description/</a><br />
no limit on how many times a digit can be reused.</p>
<p>You may assume the given input string is always valid. For example, &#8220;01:34&#8221;, &#8220;12:09&#8221; are all valid. &#8220;1:34&#8221;, &#8220;12:9&#8221; are all invalid.</p>
<h2><b>Example 1:</b></h2>
<pre class="crayon:false">Input: "19:34"
Output: "19:39"
Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, 
which occurs 5 minutes later.  
It is not 19:33, because this occurs 23 hours and 59 minutes later.
</pre>
<h2><b>Example 2:</b></h2>
<pre class="crayon:false">Input: "23:59"
Output: "22:22"
Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. 
It may be assumed that the returned time is next day's time since it is smaller 
than the input time numerically.</pre>
<p>Ads<br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins></p>
<h1><strong>Solution1: Brute force</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 3 ms
class Solution {
public:
    string nextClosestTime(string time) {
        set&lt;char&gt; s(time.begin(), time.end());
        int hour = stoi(time.substr(0, 2));
        int min = stoi(time.substr(3, 2));
        while (true) {
            if (++min == 60) {
                min = 0;
                (++hour) %= 24;                
            }
            char buffer[5];
            sprintf(buffer, "%02d:%02d", hour, min);
            set&lt;char&gt; s2(buffer, buffer + sizeof(buffer));
            if (includes(s.begin(), s.end(), s2.begin(), s2.end()))
                return string(buffer);
        }
        return time;
    }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 85 ms
class Solution {
    public String nextClosestTime(String time) {
        int hour = Integer.parseInt(time.substring(0, 2));
        int min = Integer.parseInt(time.substring(3, 5));
        while (true) {            
            if (++min == 60) {
                min = 0;
                ++hour;
                hour %= 24;
            }
            String curr = String.format("%02d:%02d", hour, min);
            Boolean valid = true;
            for (int i = 0; i &lt; curr.length(); ++i)
                if (time.indexOf(curr.charAt(i)) &lt; 0) {
                    valid = false;
                    break;
                }
            if (valid) return curr;
        }
    }
}</pre><p></div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 65 ms
"""
class Solution:
    def nextClosestTime(self, time):        
        s = set(time)
        hour = int(time[0:2])
        minute = int(time[3:5])
        while True:
            minute += 1
            if minute == 60:
                minute = 0
                hour = 0 if hour == 23 else hour + 1
            
            time = "%02d:%02d" % (hour, minute)
            if set(time) &lt;= s:
                return time
        return time</pre><p></div></div></p>
<h1><strong>Solution 2: DFS</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua 
// Running time: 3 ms
class Solution {
public:
    string nextClosestTime(string time) {
        vector&lt;int&gt; d = {
            time[0] - '0', time[1] - '0', time[3] - '0', time[4] - '0' };
        
        int h = d[0] * 10 + d[1];
        int m = d[2] * 10 + d[3];
        vector&lt;int&gt; curr(4, 0);
        int now = toTime(h, m);
        int best = now;
        
        dfs(0, d, curr, now, best);
        char buff[5];
        sprintf(buff, "%02d:%02d", best / 60, best % 60);
        return string(buff);
    }
private:
    void dfs(int dep, const vector&lt;int&gt;&amp; digits, vector&lt;int&gt;&amp; curr, int time, int&amp; best) {
        if (dep == 4) {
            int curr_h = curr[0] * 10 + curr[1];
            int curr_m = curr[2] * 10 + curr[3];
            if (curr_h &gt; 23 || curr_m &gt; 59) return;
            int curr_time = toTime(curr_h, curr_m);
            if (timeDiff(time, curr_time) &lt; timeDiff(time, best))
                best = curr_time;
            return;
        }            
        
        for (int digit : digits) {
            curr[dep] = digit;
            dfs(dep + 1, digits, curr, time, best);
        }        
    }
    
    int toTime(int h, int m) {
        return h * 60 + m;
    }
    
    int timeDiff(int t1, int t2) {
        if (t1 == t2) return INT_MAX;
        return ((t2 - t1) + 24 * 60) % (24 * 60);
    }
};</pre><p></div></div></p>
<h1><strong>Solution 3: Brute force + Time library</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 292 ms
"""
from datetime import *
class Solution:
    def nextClosestTime(self, time):        
        s = set(time)        
        while True:
            time = (datetime.strptime(time, '%H:%M') + 
                    timedelta(minutes=1)).strftime('%H:%M')
            if set(time) &lt;= s: 
                return time
        return time</pre><p></div></div></p>
<h1>Related Problems:</h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/bit/leetcode-401-binary-watch/">花花酱 LeetCode 401. Binary Watch</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-681-next-closest-time/">花花酱 LeetCode 681. Next Closest Time</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-681-next-closest-time/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
