<?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>date Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/date/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/date/</link>
	<description></description>
	<lastBuildDate>Sat, 17 Sep 2022 19:56:20 +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>date Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/date/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2409. Count Days Spent Together</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2409-count-days-spent-together/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2409-count-days-spent-together/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 17 Sep 2022 19:55:26 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[year]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9824</guid>

					<description><![CDATA[<p>Alice and Bob are traveling to Rome for separate business meetings. You are given 4 strings&#160;arriveAlice,&#160;leaveAlice,&#160;arriveBob, and&#160;leaveBob. Alice will be in the city from the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-2409-count-days-spent-together/">花花酱 LeetCode 2409. Count Days Spent Together</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>Alice and Bob are traveling to Rome for separate business meetings.</p>



<p>You are given 4 strings&nbsp;<code>arriveAlice</code>,&nbsp;<code>leaveAlice</code>,&nbsp;<code>arriveBob</code>, and&nbsp;<code>leaveBob</code>. Alice will be in the city from the dates&nbsp;<code>arriveAlice</code>&nbsp;to&nbsp;<code>leaveAlice</code>&nbsp;(<strong>inclusive</strong>), while Bob will be in the city from the dates&nbsp;<code>arriveBob</code>&nbsp;to&nbsp;<code>leaveBob</code>&nbsp;(<strong>inclusive</strong>). Each will be a 5-character string in the format&nbsp;<code>"MM-DD"</code>, corresponding to the month and day of the date.</p>



<p>Return<em>&nbsp;the total number of days that Alice and Bob are in Rome together.</em></p>



<p>You can assume that all dates occur in the&nbsp;<strong>same</strong>&nbsp;calendar year, which is&nbsp;<strong>not</strong>&nbsp;a leap year. Note that the number of days per month can be represented as:&nbsp;<code>[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19"
<strong>Output:</strong> 3
<strong>Explanation:</strong> Alice will be in Rome from August 15 to August 18. Bob will be in Rome from August 16 to August 19. They are both in Rome together on August 16th, 17th, and 18th, so the answer is 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arriveAlice = "10-01", leaveAlice = "10-31", arriveBob = "11-01", leaveBob = "12-31"
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no day when Alice and Bob are in Rome together, so we return 0.
</pre>



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



<ul><li>All dates are provided in the format&nbsp;<code>"MM-DD"</code>.</li><li>Alice and Bob&#8217;s arrival dates are&nbsp;<strong>earlier than or equal to</strong>&nbsp;their leaving dates.</li><li>The given dates are valid dates of a&nbsp;<strong>non-leap</strong>&nbsp;year.</li></ul>



<h2><strong>Solution: Math</strong></h2>



<p>Convert date to days of the year.</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:
  int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob) {
    array&lt;int, 12&gt; days{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    auto getDay = [&amp;](string date) {
      int mm = (date[0] - '0') * 10 + (date[1] - '0');
      int dd = (date[3] - '0') * 10 + (date[4] - '0');
      return accumulate(begin(days), begin(days) + mm - 1, dd);
    };
    
    int s = max(getDay(arriveAlice), getDay(arriveBob));
    int e = min(getDay(leaveAlice), getDay(leaveBob));
    return e &gt;= s ? e - s + 1 : 0;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-2409-count-days-spent-together/">花花酱 LeetCode 2409. Count Days Spent Together</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-2409-count-days-spent-together/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1751. Maximum Number of Events That Can Be Attended II</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1751-maximum-number-of-events-that-can-be-attended-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1751-maximum-number-of-events-that-can-be-attended-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 06 Feb 2021 16:37:38 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[range]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8071</guid>

					<description><![CDATA[<p>You are given an array of&#160;events&#160;where&#160;events[i] = [startDayi, endDayi, valuei]. The&#160;ith&#160;event starts at&#160;startDayiand ends at&#160;endDayi, and if you attend this event, you will receive a&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1751-maximum-number-of-events-that-can-be-attended-ii/">花花酱 LeetCode 1751. Maximum Number of Events That Can Be Attended II</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 an array of&nbsp;<code>events</code>&nbsp;where&nbsp;<code>events[i] = [startDay<sub>i</sub>, endDay<sub>i</sub>, value<sub>i</sub>]</code>. The&nbsp;<code>i<sup>th</sup></code>&nbsp;event starts at&nbsp;<code>startDay<sub>i</sub></code>and ends at&nbsp;<code>endDay<sub>i</sub></code>, and if you attend this event, you will receive a value of&nbsp;<code>value<sub>i</sub></code>. You are also given an integer&nbsp;<code>k</code>&nbsp;which represents the maximum number of events you can attend.</p>



<p>You can only attend one event at a time. If you choose to attend an event, you must attend the&nbsp;<strong>entire</strong>&nbsp;event. Note that the end day is&nbsp;<strong>inclusive</strong>: that is, you cannot attend two events where one of them starts and the other ends on the same day.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>maximum sum</strong>&nbsp;of values that you can receive by attending events.</em></p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60048-pm.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> events = [[1,2,4],[3,4,3],[2,3,1]], k = 2
<strong>Output:</strong> 7
<strong>Explanation: </strong>Choose the green events, 0 and 1 (0-indexed) for a total value of 4 + 3 = 7.</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60150-pm.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> events = [[1,2,4],[3,4,3],[2,3,10]], k = 2
<strong>Output:</strong> 10
<strong>Explanation:</strong> Choose event 2 for a total value of 10.
Notice that you cannot attend any other event as they overlap, and that you do <strong>not</strong> have to attend k events.</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60703-pm.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3
<strong>Output:</strong> 9
<strong>Explanation:</strong> Although the events do not overlap, you can only attend 3 events. Pick the highest valued three.</pre>



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



<ul><li><code>1 &lt;= k &lt;= events.length</code></li><li><code>1 &lt;= k * events.length &lt;= 10<sup>6</sup></code></li><li><code>1 &lt;= startDay<sub>i</sub>&nbsp;&lt;= endDay<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li><li><code>1 &lt;= value<sub>i</sub>&nbsp;&lt;= 10<sup>6</sup></code></li></ul>



<h2><strong>Solution: DP + Binary Search</strong></h2>



<p>Sort events by ending time.<br>dp[i][j] := max value we can get by attending <strong><span class="has-inline-color has-vivid-red-color">at most</span></strong> j events among events[0~i].<br>dp[i][j] = max(dp[i &#8211; 1][j],  dp[p][j &#8211; 1] + value[i])<br>p is the first event that does not overlap with the current one.</p>



<p>Time complexity: O(nlogn + nk)<br>Space complexity: O(nk)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxValue(vector&lt;vector&lt;int&gt;&gt;&amp; events, int k) {
    const int n = events.size(); 
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(k + 1));    
    vector&lt;int&gt; e(n);
    auto comp = [](const vector&lt;int&gt;&amp; a, const vector&lt;int&gt;&amp; b) {
      return a[1] &lt; b[1];
    };
    
    sort(begin(events), end(events), comp);
    
    for (int i = 1; i &lt;= n; ++i) {
      const int p = lower_bound(begin(events), 
                                begin(events) + i, 
                                vector&lt;int&gt;{0, events[i - 1][0], 0},
                                comp) - begin(events);
      for (int j = 1; j &lt;= k; ++j)
        dp[i][j] = max(dp[i - 1][j], 
                       dp[p][j - 1] + events[i - 1][2]);
    }    
    return dp[n][k];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1751-maximum-number-of-events-that-can-be-attended-ii/">花花酱 LeetCode 1751. Maximum Number of Events That Can Be Attended II</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/dynamic-programming/leetcode-1751-maximum-number-of-events-that-can-be-attended-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1507. Reformat Date</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1507-reformat-date/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1507-reformat-date/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Jul 2020 03:58:04 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7067</guid>

					<description><![CDATA[<p>Given a&#160;date&#160;string in the form&#160;Day Month Year, where: Day&#160;is in the set&#160;{"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}. Month&#160;is in the set&#160;{"Jan", "Feb", "Mar", "Apr",&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1507-reformat-date/">花花酱 LeetCode 1507. Reformat Date</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 a&nbsp;<code>date</code>&nbsp;string in the form&nbsp;<code>Day Month Year</code>, where:</p>



<ul><li><code>Day</code>&nbsp;is in the set&nbsp;<code>{"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}</code>.</li><li><code>Month</code>&nbsp;is in the set&nbsp;<code>{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}</code>.</li><li><code>Year</code>&nbsp;is in the range&nbsp;<code>[1900, 2100]</code>.</li></ul>



<p>Convert the date string to the format&nbsp;<code>YYYY-MM-DD</code>, where:</p>



<ul><li><code>YYYY</code>&nbsp;denotes the 4 digit year.</li><li><code>MM</code>&nbsp;denotes the 2 digit month.</li><li><code>DD</code>&nbsp;denotes the 2 digit day.</li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> date = "20th Oct 2052"
<strong>Output:</strong> "2052-10-20"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> date = "6th Jun 1933"
<strong>Output:</strong> "1933-06-06"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> date = "26th May 1960"
<strong>Output:</strong> "1960-05-26"
</pre>



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



<ul><li>The given dates are guaranteed to be valid, so no error handling is necessary.</li></ul>



<h2><strong>Solution: String + HashTable</strong></h2>



<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 reformatDate(string date) {
    stringstream ss(date);
    string day, month, year;
    ss &gt;&gt; day &gt;&gt; month &gt;&gt; year;
    unordered_map&lt;string, string&gt; m{{&quot;Jan&quot;, &quot;01&quot;},
                                    {&quot;Feb&quot;, &quot;02&quot;},
                                    {&quot;Mar&quot;, &quot;03&quot;},
                                    {&quot;Apr&quot;, &quot;04&quot;},
                                    {&quot;May&quot;, &quot;05&quot;},
                                    {&quot;Jun&quot;, &quot;06&quot;},
                                    {&quot;Jul&quot;, &quot;07&quot;},
                                    {&quot;Aug&quot;, &quot;08&quot;},
                                    {&quot;Sep&quot;, &quot;09&quot;},
                                    {&quot;Oct&quot;, &quot;10&quot;},
                                    {&quot;Nov&quot;, &quot;11&quot;},
                                    {&quot;Dec&quot;, &quot;12&quot;}};
    day = day.substr(0, day.length() - 2);
    if (day.length() == 1) day = &quot;0&quot; + day;
    return year + &quot;-&quot; + m[month] + &quot;-&quot; + day;
  }
};</pre>

</div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
  public String reformatDate(String date) {
    Map&lt;String, String&gt; m = new HashMap&lt;String, String&gt;();
    m.put(&quot;Jan&quot;, &quot;01&quot;);
    m.put(&quot;Feb&quot;, &quot;02&quot;);
    m.put(&quot;Mar&quot;, &quot;03&quot;);
    m.put(&quot;Apr&quot;, &quot;04&quot;);
    m.put(&quot;May&quot;, &quot;05&quot;);
    m.put(&quot;Jun&quot;, &quot;06&quot;);
    m.put(&quot;Jul&quot;, &quot;07&quot;);
    m.put(&quot;Aug&quot;, &quot;08&quot;);
    m.put(&quot;Sep&quot;, &quot;09&quot;);
    m.put(&quot;Oct&quot;, &quot;10&quot;);
    m.put(&quot;Nov&quot;, &quot;11&quot;);
    m.put(&quot;Dec&quot;, &quot;12&quot;);
    String[] items = date.split(&quot; &quot;);    
    String day = items[0].substring(0, items[0].length() - 2);
    if (day.length() == 1) day = &quot;0&quot; + day;
    return items[2] + &quot;-&quot; + m.get(items[1]) + &quot;-&quot; + day;
  }
}</pre>

</div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def reformatDate(self, date: str) -&gt; str:
    m = {&quot;Jan&quot;: &quot;01&quot;, &quot;Feb&quot;: &quot;02&quot;, &quot;Mar&quot;: &quot;03&quot;, 
         &quot;Apr&quot;: &quot;04&quot;, &quot;May&quot;: &quot;05&quot;, &quot;Jun&quot;: &quot;06&quot;, 
         &quot;Jul&quot;: &quot;07&quot;, &quot;Aug&quot;: &quot;08&quot;, &quot;Sep&quot;: &quot;09&quot;, 
         &quot;Oct&quot;: &quot;10&quot;, &quot;Nov&quot;: &quot;11&quot;, &quot;Dec&quot;: &quot;12&quot;}
    items = date.split(&quot; &quot;)
    day = items[0][:-2]
    if len(day) == 1: day = &quot;0&quot; + day
    return items[2] + &quot;-&quot; + m[items[1]] + &quot;-&quot; + day</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1507-reformat-date/">花花酱 LeetCode 1507. Reformat Date</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-1507-reformat-date/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1360. Number of Days Between Two Dates</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1360-number-of-days-between-two-dates/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1360-number-of-days-between-two-dates/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 23 Feb 2020 09:20:02 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6367</guid>

					<description><![CDATA[<p>Write a program to count the number of days between two dates. The two dates are given as strings, their format is&#160;YYYY-MM-DD&#160;as shown in the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1360-number-of-days-between-two-dates/">花花酱 LeetCode 1360. Number of Days Between Two Dates</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>Write a program to count the number of days between two dates.</p>



<p>The two dates are given as strings, their format is&nbsp;<code>YYYY-MM-DD</code>&nbsp;as shown in the examples.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> date1 = "2019-06-29", date2 = "2019-06-30"
<strong>Output:</strong> 1
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> date1 = "2020-01-15", date2 = "2019-12-31"
<strong>Output:</strong> 15
</pre>



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



<ul><li>The given dates are valid&nbsp;dates between the years&nbsp;<code>1971</code>&nbsp;and&nbsp;<code>2100</code>.</li></ul>



<h2><strong>Solution: Convert to days since epoch</strong></h2>



<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 daysBetweenDates(string date1, string date2) {
    array&lt;int, 12&gt; m{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    auto isLeap = [](int year) {
      return year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0);
    };
    auto daysFromEpoch = [&amp;](const string&amp; date) {
      int year = stoi(date.substr(0, 4));
      int month = stoi(date.substr(5, 2));
      int days = stoi(date.substr(8, 2));      
      for (int i = 1970; i &lt; year; ++i)
        days += 365 + isLeap(i);
      for (int i = 1; i &lt; month; ++i)
        days += m[i - 1];
      days += month &gt; 2 &amp;&amp; isLeap(year);
      return days;
    };
    return abs(daysFromEpoch(date1) - daysFromEpoch(date2));
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1360-number-of-days-between-two-dates/">花花酱 LeetCode 1360. Number of Days Between Two Dates</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-1360-number-of-days-between-two-dates/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1185. Day of the Week</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1185-day-of-the-week/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1185-day-of-the-week/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 08 Sep 2019 04:18:17 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[leap year]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5531</guid>

					<description><![CDATA[<p>Given a date, return the corresponding day of the week for that date. The input is given as three integers representing the&#160;day,&#160;month&#160;and&#160;year&#160;respectively. Return the answer&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1185-day-of-the-week/">花花酱 LeetCode 1185. Day of the Week</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 a date, return the corresponding day of the week for that date.</p>



<p>The input is given as three integers representing the&nbsp;<code>day</code>,&nbsp;<code>month</code>&nbsp;and&nbsp;<code>year</code>&nbsp;respectively.</p>



<p>Return the answer as one of the following values&nbsp;<code>{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> day = 31, month = 8, year = 2019
<strong>Output:</strong> "Saturday"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> day = 18, month = 7, year = 1999
<strong>Output:</strong> "Sunday"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> day = 15, month = 8, year = 1993
<strong>Output:</strong> "Sunday"
</pre>



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



<ul><li>The given dates are valid&nbsp;dates between the years&nbsp;<code>1971</code>&nbsp;and&nbsp;<code>2100</code>.</li></ul>



<p><strong>Solution: LeapYear</strong></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
inline bool leapYear(int y) {
  return (y % 4 == 0 &amp;&amp; y % 100 != 0) || (y % 400 == 0);
}
class Solution {
public:
  string dayOfTheWeek(int day, int month, int year) {    
    vector&lt;string&gt; names = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};
    vector&lt;int&gt; days = {31, 28 + leapYear(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int sum = 0;
    for (int i = 1970; i &lt; year; ++i)
      sum += 365 + leapYear(i);    
    for (int i = 1; i &lt; month; ++i)
      sum += days[i - 1];    
    sum += day;
    return names[(sum + 3) % 7];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1185-day-of-the-week/">花花酱 LeetCode 1185. Day of the Week</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-1185-day-of-the-week/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
