<?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>loop Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/loop/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/loop/</link>
	<description></description>
	<lastBuildDate>Sat, 09 Jan 2021 22:47:29 +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>loop Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/loop/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1716. Calculate Money in Leetcode Bank</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1716-calculate-money-in-leetcode-bank/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1716-calculate-money-in-leetcode-bank/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 09 Jan 2021 22:43:56 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7931</guid>

					<description><![CDATA[<p>Hercy wants to save money for his first car. He puts money in the Leetcode&#160;bank&#160;every day. He starts by putting in&#160;$1&#160;on Monday, the first day.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1716-calculate-money-in-leetcode-bank/">花花酱 LeetCode 1716. Calculate Money in Leetcode Bank</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>Hercy wants to save money for his first car. He puts money in the Leetcode&nbsp;bank&nbsp;<strong>every day</strong>.</p>



<p>He starts by putting in&nbsp;<code>$1</code>&nbsp;on Monday, the first day. Every day from Tuesday to Sunday, he will put in&nbsp;<code>$1</code>&nbsp;more than the day before. On every subsequent Monday, he will put in&nbsp;<code>$1</code>&nbsp;more than the&nbsp;<strong>previous Monday</strong>.</p>



<p>Given&nbsp;<code>n</code>, return&nbsp;<em>the total amount of money he will have in the Leetcode bank at the end of the&nbsp;</em><code>n<sup>th</sup></code><em>&nbsp;day.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4
<strong>Output:</strong> 10
<strong>Explanation:</strong>&nbsp;After the 4<sup>th</sup> day, the total is 1 + 2 + 3 + 4 = 10.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 10
<strong>Output:</strong> 37
<strong>Explanation:</strong>&nbsp;After the 10<sup>th</sup> day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2<sup>nd</sup> Monday, Hercy only puts in $2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 20
<strong>Output:</strong> 96
<strong>Explanation:</strong>&nbsp;After the 20<sup>th</sup> day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
</pre>



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



<ul><li><code>1 &lt;= n &lt;= 1000</code></li></ul>



<h2><strong>Solution 1: Simulation</strong></h2>



<p>Increase the amount by 1 everyday, the decrease 6 after every sunday.</p>



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



<p></p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
 
<pre class="crayon-plain-tag">// Author: huahua
class Solution {
public:
  int totalMoney(int n) {
    int total = 0;
    for (int d = 0, m = 1; d &lt; n; ++d) {
      total += m++;
      if (d % 7 == 6) m -= 6;
    }
    return total;
  }
};</pre>
</div></div>



<p></p>



<p>Could also be solved using Math in O(1)</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1716-calculate-money-in-leetcode-bank/">花花酱 LeetCode 1716. Calculate Money in Leetcode Bank</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-1716-calculate-money-in-leetcode-bank/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
