1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-06 07:51:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2023-12-09 19:57:46 +08:00
parent 9bc4722a45
commit 3770b44d1e
4792 changed files with 10889 additions and 10886 deletions

View File

@@ -7,7 +7,7 @@
"boundTopicId": 1422461,
"title": "Design an ATM Machine",
"titleSlug": "design-an-atm-machine",
"content": "<p>There is an ATM machine that stores banknotes of <code>5</code> denominations: <code>20</code>, <code>50</code>, <code>100</code>, <code>200</code>, and <code>500</code> dollars. Initially the ATM is empty. The user can use the machine to deposit or withdraw any amount of money.</p>\n\n<p>When withdrawing, the machine prioritizes using banknotes of <strong>larger</strong> values.</p>\n\n<ul>\n\t<li>For example, if you want to withdraw <code>$300</code> and there are <code>2</code> <code>$50</code> banknotes, <code>1</code> <code>$100</code> banknote, and <code>1</code> <code>$200</code> banknote, then the machine will use the <code>$100</code> and <code>$200</code> banknotes.</li>\n\t<li>However, if you try to withdraw <code>$600</code> and there are <code>3</code> <code>$200</code> banknotes and <code>1</code> <code>$500</code> banknote, then the withdraw request will be rejected because the machine will first try to use the <code>$500</code> banknote and then be unable to use banknotes to complete the remaining <code>$100</code>. Note that the machine is <strong>not</strong> allowed to use the <code>$200</code> banknotes instead of the <code>$500</code> banknote.</li>\n</ul>\n\n<p>Implement the ATM class:</p>\n\n<ul>\n\t<li><code>ATM()</code> Initializes the ATM object.</li>\n\t<li><code>void deposit(int[] banknotesCount)</code> Deposits new banknotes in the order <code>$20</code>, <code>$50</code>, <code>$100</code>, <code>$200</code>, and <code>$500</code>.</li>\n\t<li><code>int[] withdraw(int amount)</code> Returns an array of length <code>5</code> of the number of banknotes that will be handed to the user in the order <code>$20</code>, <code>$50</code>, <code>$100</code>, <code>$200</code>, and <code>$500</code>, and update the number of banknotes in the ATM after withdrawing. Returns <code>[-1]</code> if it is not possible (do <strong>not</strong> withdraw any banknotes in this case).</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n[&quot;ATM&quot;, &quot;deposit&quot;, &quot;withdraw&quot;, &quot;deposit&quot;, &quot;withdraw&quot;, &quot;withdraw&quot;]\n[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]]\n<strong>Output</strong>\n[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]]\n\n<strong>Explanation</strong>\nATM atm = new ATM();\natm.deposit([0,0,1,2,1]); // Deposits 1 $100 banknote, 2 $200 banknotes,\n // and 1 $500 banknote.\natm.withdraw(600); // Returns [0,0,1,0,1]. The machine uses 1 $100 banknote\n // and 1 $500 banknote. The banknotes left over in the\n // machine are [0,0,0,2,0].\natm.deposit([0,1,0,1,1]); // Deposits 1 $50, $200, and $500 banknote.\n // The banknotes in the machine are now [0,1,0,3,1].\natm.withdraw(600); // Returns [-1]. The machine will try to use a $500 banknote\n // and then be unable to complete the remaining $100,\n // so the withdraw request will be rejected.\n // Since the request is rejected, the number of banknotes\n // in the machine is not modified.\natm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknote\n // and 1 $500 banknote.</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>banknotesCount.length == 5</code></li>\n\t<li><code>0 &lt;= banknotesCount[i] &lt;= 10<sup>9</sup></code></li>\n\t<li><code>1 &lt;= amount &lt;= 10<sup>9</sup></code></li>\n\t<li>At most <code>5000</code> calls <strong>in total</strong> will be made to <code>withdraw</code> and <code>deposit</code>.</li>\n\t<li>At least <strong>one</strong> call will be made to each function <code>withdraw</code> and <code>deposit</code>.</li>\n</ul>\n",
"content": "<p>There is an ATM machine that stores banknotes of <code>5</code> denominations: <code>20</code>, <code>50</code>, <code>100</code>, <code>200</code>, and <code>500</code> dollars. Initially the ATM is empty. The user can use the machine to deposit or withdraw any amount of money.</p>\n\n<p>When withdrawing, the machine prioritizes using banknotes of <strong>larger</strong> values.</p>\n\n<ul>\n\t<li>For example, if you want to withdraw <code>$300</code> and there are <code>2</code> <code>$50</code> banknotes, <code>1</code> <code>$100</code> banknote, and <code>1</code> <code>$200</code> banknote, then the machine will use the <code>$100</code> and <code>$200</code> banknotes.</li>\n\t<li>However, if you try to withdraw <code>$600</code> and there are <code>3</code> <code>$200</code> banknotes and <code>1</code> <code>$500</code> banknote, then the withdraw request will be rejected because the machine will first try to use the <code>$500</code> banknote and then be unable to use banknotes to complete the remaining <code>$100</code>. Note that the machine is <strong>not</strong> allowed to use the <code>$200</code> banknotes instead of the <code>$500</code> banknote.</li>\n</ul>\n\n<p>Implement the ATM class:</p>\n\n<ul>\n\t<li><code>ATM()</code> Initializes the ATM object.</li>\n\t<li><code>void deposit(int[] banknotesCount)</code> Deposits new banknotes in the order <code>$20</code>, <code>$50</code>, <code>$100</code>, <code>$200</code>, and <code>$500</code>.</li>\n\t<li><code>int[] withdraw(int amount)</code> Returns an array of length <code>5</code> of the number of banknotes that will be handed to the user in the order <code>$20</code>, <code>$50</code>, <code>$100</code>, <code>$200</code>, and <code>$500</code>, and update the number of banknotes in the ATM after withdrawing. Returns <code>[-1]</code> if it is not possible (do <strong>not</strong> withdraw any banknotes in this case).</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n[&quot;ATM&quot;, &quot;deposit&quot;, &quot;withdraw&quot;, &quot;deposit&quot;, &quot;withdraw&quot;, &quot;withdraw&quot;]\n[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]]\n<strong>Output</strong>\n[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]]\n\n<strong>Explanation</strong>\nATM atm = new ATM();\natm.deposit([0,0,1,2,1]); // Deposits 1 $100 banknote, 2 $200 banknotes,\n // and 1 $500 banknote.\natm.withdraw(600); // Returns [0,0,1,0,1]. The machine uses 1 $100 banknote\n // and 1 $500 banknote. The banknotes left over in the\n // machine are [0,0,0,2,0].\natm.deposit([0,1,0,1,1]); // Deposits 1 $50, $200, and $500 banknote.\n // The banknotes in the machine are now [0,1,0,3,1].\natm.withdraw(600); // Returns [-1]. The machine will try to use a $500 banknote\n // and then be unable to complete the remaining $100,\n // so the withdraw request will be rejected.\n // Since the request is rejected, the number of banknotes\n // in the machine is not modified.\natm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknote\n // and 1 $500 banknote.</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>banknotesCount.length == 5</code></li>\n\t<li><code>0 &lt;= banknotesCount[i] &lt;= 10<sup>9</sup></code></li>\n\t<li><code>1 &lt;= amount &lt;= 10<sup>9</sup></code></li>\n\t<li>At most <code>5000</code> calls <strong>in total</strong> will be made to <code>withdraw</code> and <code>deposit</code>.</li>\n\t<li>At least <strong>one</strong> call will be made to each function <code>withdraw</code> and <code>deposit</code>.</li>\n\t<li>Sum of <code>banknotesCount[i]</code> in all deposits doesn&#39;t exceed <code>10<sup>9</sup></code></li>\n</ul>\n",
"translatedTitle": "设计一个 ATM 机器",
"translatedContent": "<p>一个 ATM 机器,存有&nbsp;<code>5</code>&nbsp;种面值的钞票:<code>20</code>&nbsp;<code>50</code>&nbsp;<code>100</code>&nbsp;<code>200</code>&nbsp;和&nbsp;<code>500</code>&nbsp;美元。初始时ATM 机是空的。用户可以用它存或者取任意数目的钱。</p>\n\n<p>取款时,机器会优先取 <b>较大</b>&nbsp;数额的钱。</p>\n\n<ul>\n\t<li>比方说,你想取&nbsp;<code>$300</code>&nbsp;,并且机器里有&nbsp;<code>2</code>&nbsp;张 <code>$50</code>&nbsp;的钞票,<code>1</code>&nbsp;张&nbsp;<code>$100</code>&nbsp;的钞票和<code>1</code>&nbsp;张&nbsp;<code>$200</code>&nbsp;的钞票,那么机器会取出&nbsp;<code>$100</code> 和&nbsp;<code>$200</code>&nbsp;的钞票。</li>\n\t<li>但是,如果你想取&nbsp;<code>$600</code>&nbsp;,机器里有&nbsp;<code>3</code>&nbsp;张&nbsp;<code>$200</code>&nbsp;的钞票和<code>1</code>&nbsp;张&nbsp;<code>$500</code>&nbsp;的钞票,那么取款请求会被拒绝,因为机器会先取出&nbsp;<code>$500</code>&nbsp;的钞票,然后无法取出剩余的&nbsp;<code>$100</code>&nbsp;。注意,因为有&nbsp;<code>$500</code>&nbsp;钞票的存在,机器&nbsp;<strong>不能</strong>&nbsp;取&nbsp;<code>$200</code>&nbsp;的钞票。</li>\n</ul>\n\n<p>请你实现 ATM 类:</p>\n\n<ul>\n\t<li><code>ATM()</code>&nbsp;初始化 ATM 对象。</li>\n\t<li><code>void deposit(int[] banknotesCount)</code>&nbsp;分别存入&nbsp;<code>$20</code>&nbsp;<code>$50</code><code>$100</code><code>$200</code>&nbsp;和&nbsp;<code>$500</code>&nbsp;钞票的数目。</li>\n\t<li><code>int[] withdraw(int amount)</code>&nbsp;返回一个长度为&nbsp;<code>5</code>&nbsp;的数组,分别表示&nbsp;<code>$20</code>&nbsp;<code>$50</code><code>$100</code>&nbsp;<code>$200</code>&nbsp;和&nbsp;<code>$500</code>&nbsp;钞票的数目,并且更新 ATM 机里取款后钞票的剩余数量。如果无法取出指定数额的钱,请返回&nbsp;<code>[-1]</code>&nbsp;(这种情况下 <strong>不</strong>&nbsp;取出任何钞票)。</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1</strong></p>\n\n<pre>\n<strong>输入:</strong>\n[\"ATM\", \"deposit\", \"withdraw\", \"deposit\", \"withdraw\", \"withdraw\"]\n[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]]\n<strong>输出:</strong>\n[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]]\n\n<strong>解释:</strong>\nATM atm = new ATM();\natm.deposit([0,0,1,2,1]); // 存入 1 张 $100 2 张 $200 和 1 张 $500 的钞票。\natm.withdraw(600); // 返回 [0,0,1,0,1] 。机器返回 1 张 $100 和 1 张 $500 的钞票。机器里剩余钞票的数量为 [0,0,0,2,0] 。\natm.deposit([0,1,0,1,1]); // 存入 1 张 $50 1 张 $200 和 1 张 $500 的钞票。\n // 机器中剩余钞票数量为 [0,1,0,3,1] 。\natm.withdraw(600); // 返回 [-1] 。机器会尝试取出 $500 的钞票,然后无法得到剩余的 $100 ,所以取款请求会被拒绝。\n // 由于请求被拒绝,机器中钞票的数量不会发生改变。\natm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的钞票和 1 张 $500 的钞票。</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>banknotesCount.length == 5</code></li>\n\t<li><code>0 &lt;= banknotesCount[i] &lt;= 10<sup>9</sup></code></li>\n\t<li><code>1 &lt;= amount &lt;= 10<sup>9</sup></code></li>\n\t<li><strong>总共</strong>&nbsp;最多有&nbsp;<code>5000</code>&nbsp;次&nbsp;<code>withdraw</code> 和&nbsp;<code>deposit</code>&nbsp;的调用。</li>\n\t<li><span style=\"\">函数 </span><code>withdraw</code> 和&nbsp;<code>deposit</code>&nbsp;至少各有 <strong>一次&nbsp;</strong>调用。</li>\n</ul>\n",
"isPaidOnly": false,
@@ -155,7 +155,7 @@
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"7.3K\", \"totalSubmission\": \"19.9K\", \"totalAcceptedRaw\": 7327, \"totalSubmissionRaw\": 19897, \"acRate\": \"36.8%\"}",
"stats": "{\"totalAccepted\": \"7.3K\", \"totalSubmission\": \"19.9K\", \"totalAcceptedRaw\": 7328, \"totalSubmissionRaw\": 19900, \"acRate\": \"36.8%\"}",
"hints": [
"Store the number of banknotes of each denomination.",
"Can you use math to quickly evaluate a withdrawal request?"