{"id":7931,"date":"2021-01-09T14:43:56","date_gmt":"2021-01-09T22:43:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7931"},"modified":"2021-01-09T14:47:29","modified_gmt":"2021-01-09T22:47:29","slug":"leetcode-1716-calculate-money-in-leetcode-bank","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1716-calculate-money-in-leetcode-bank\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1716. Calculate Money in Leetcode Bank"},"content":{"rendered":"\n<p>Hercy wants to save money for his first car. He puts money in the Leetcode&nbsp;bank&nbsp;<strong>every day<\/strong>.<\/p>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4\n<strong>Output:<\/strong> 10\n<strong>Explanation:<\/strong>&nbsp;After the 4<sup>th<\/sup> day, the total is 1 + 2 + 3 + 4 = 10.\n<\/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> n = 10\n<strong>Output:<\/strong> 37\n<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.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 20\n<strong>Output:<\/strong> 96\n<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.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Simulation<\/strong><\/h2>\n\n\n\n<p>Increase the amount by 1 everyday, the decrease 6 after every sunday.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n \n<pre>\/\/ Author: huahua\nclass Solution {\npublic:\n  int totalMoney(int n) {\n    int total = 0;\n    for (int d = 0, m = 1; d < n; ++d) {\n      total += m++;\n      if (d % 7 == 6) m -= 6;\n    }\n    return total;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Could also be solved using Math in O(1)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hercy wants to save money for his first car. He puts money in the Leetcode&nbsp;bank&nbsp;every day. He starts by putting in&nbsp;$1&nbsp;on Monday, the first day.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,685,179],"class_list":["post-7931","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-loop","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7931","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=7931"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7931\/revisions"}],"predecessor-version":[{"id":7939,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7931\/revisions\/7939"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}