{"id":8607,"date":"2021-10-17T20:43:12","date_gmt":"2021-10-18T03:43:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8607"},"modified":"2021-10-17T20:44:23","modified_gmt":"2021-10-18T03:44:23","slug":"leetcode-2043-simple-bank-system","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2043-simple-bank-system\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2043. Simple Bank System"},"content":{"rendered":"\n<p>You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has&nbsp;<code>n<\/code>&nbsp;accounts numbered from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>. The initial balance of each account is stored in a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>balance<\/code>, with the&nbsp;<code>(i + 1)<sup>th<\/sup><\/code>&nbsp;account having an initial balance of&nbsp;<code>balance[i]<\/code>.<\/p>\n\n\n\n<p>Execute all the&nbsp;<strong>valid<\/strong>&nbsp;transactions. A transaction is&nbsp;<strong>valid<\/strong>&nbsp;if:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The given account number(s) are between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>n<\/code>, and<\/li><li>The amount of money withdrawn or transferred from is&nbsp;<strong>less than or equal<\/strong>&nbsp;to the balance of the account.<\/li><\/ul>\n\n\n\n<p>Implement the&nbsp;<code>Bank<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>Bank(long[] balance)<\/code>&nbsp;Initializes the object with the&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>balance<\/code>.<\/li><li><code>boolean transfer(int account1, int account2, long money)<\/code>&nbsp;Transfers&nbsp;<code>money<\/code>&nbsp;dollars from the account numbered&nbsp;<code>account1<\/code>&nbsp;to the account numbered&nbsp;<code>account2<\/code>. Return&nbsp;<code>true<\/code>&nbsp;if the transaction was successful,&nbsp;<code>false<\/code>&nbsp;otherwise.<\/li><li><code>boolean deposit(int account, long money)<\/code>&nbsp;Deposit&nbsp;<code>money<\/code>&nbsp;dollars into the account numbered&nbsp;<code>account<\/code>. Return&nbsp;<code>true<\/code>&nbsp;if the transaction was successful,&nbsp;<code>false<\/code>&nbsp;otherwise.<\/li><li><code>boolean withdraw(int account, long money)<\/code>&nbsp;Withdraw&nbsp;<code>money<\/code>&nbsp;dollars from the account numbered&nbsp;<code>account<\/code>. Return&nbsp;<code>true<\/code>&nbsp;if the transaction was successful,&nbsp;<code>false<\/code>&nbsp;otherwise.<\/li><\/ul>\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[\"Bank\", \"withdraw\", \"transfer\", \"deposit\", \"transfer\", \"withdraw\"]\n[[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]\n<strong>Output<\/strong>\n<\/pre>\n\n\n<p>[null, true, true, true, false, false]<\/p>\n\n\n\n<p><strong>Explanation<\/strong> Bank bank = new Bank([10, 100, 20, 50, 30]); bank.withdraw(3, 10); \/\/ return true, account 3 has a balance of $20, so it is valid to withdraw $10. \/\/ Account 3 has $20 &#8211; $10 = $10. bank.transfer(5, 1, 20); \/\/ return true, account 5 has a balance of $30, so it is valid to transfer $20. \/\/ Account 5 has $30 &#8211; $20 = $10, and account 1 has $10 + $20 = $30. bank.deposit(5, 20); \/\/ return true, it is valid to deposit $20 to account 5. \/\/ Account 5 has $10 + $20 = $30. bank.transfer(3, 4, 15); \/\/ return false, the current balance of account 3 is $10, \/\/ so it is invalid to transfer $15 from it. bank.withdraw(10, 50); \/\/ return false, it is invalid because account 10 does not exist.<\/p>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == balance.length<\/code><\/li><li><code>1 &lt;= n, account, account1, account2 &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= balance[i], money &lt;= 10<sup>12<\/sup><\/code><\/li><li>At most&nbsp;<code>10<sup>4<\/sup><\/code>&nbsp;calls will be made to&nbsp;<strong>each<\/strong>&nbsp;function&nbsp;<code>transfer<\/code>,&nbsp;<code>deposit<\/code>,&nbsp;<code>withdraw<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(1) per operation<br>Space complexity: O(n) for n accounts<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Bank {\npublic:\n  Bank(vector<long long>& balance) \n    : n_(balance.size()), balance_(balance) {}\n\n  bool transfer(int account1, int account2, long long money) {\n    if (account1 <= 0 || account1 > n_) return false;\n    if (account2 <= 0 || account2 > n_) return false;\n    if (balance_[account1 - 1] < money) return false;\n    balance_[account1 - 1] -= money;\n    balance_[account2 - 1] += money;\n    return true;\n  }\n\n  bool deposit(int account, long long money) {\n    if (account <= 0 || account > n_) return false;\n    balance_[account - 1] += money;\n    return true;\n  }\n\n  bool withdraw(int account, long long money) {\n    if (account <= 0 || account > n_ || balance_[account - 1] < money) \n      return false;\n    balance_[account - 1] -= money;\n    return true;\n  }\nprivate:\n  const int n_;\n  vector<long long> balance_;\n};\n\n\/**\n * Your Bank object will be instantiated and called as such:\n * Bank* obj = new Bank(balance);\n * bool param_1 = obj->transfer(account1,account2,money);\n * bool param_2 = obj->deposit(account,money);\n * bool param_3 = obj->withdraw(account,money);\n *\/\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank&#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":[177,179],"class_list":["post-8607","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8607","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=8607"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8607\/revisions"}],"predecessor-version":[{"id":8609,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8607\/revisions\/8609"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}