commit f055c57fd475f4ebc5f206917621c5e5cd5a9bc7
parent 501153be1cb20b688e3fcd6fe00427897a095712
Author: Jack Sangdahl <jack@pngu.org>
Date: Sat, 25 Oct 2025 18:34:56 -0600
2043. simple bank system
Diffstat:
2 files changed, 122 insertions(+), 0 deletions(-)
diff --git a/simple_bank_system.c b/simple_bank_system.c
@@ -0,0 +1,64 @@
+// 2043. Simple Bank System
+#include "leetcode.h"
+
+/*
+ * 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 'n' accounts numbered from 1 to 'n'. the initial balance of each
+ * account is stored in a 0-indexed integer array 'balance' with the '(i + 1)'th
+ * account having an initial balance of 'balance[i]'.
+ */
+
+typedef struct {
+ long long *balance;
+ int n;
+} Bank;
+
+bool valid(Bank *obj, int account) { return account > 0 && account <= obj->n; }
+
+Bank *bankCreate(long long *balance, int balanceSize) {
+ Bank *obj = (Bank *)malloc(sizeof(Bank));
+ obj->n = balanceSize;
+ obj->balance = (long long *)malloc(balanceSize * sizeof(long long));
+ memcpy(obj->balance, balance, balanceSize);
+ return obj;
+}
+
+bool bankTransfer(Bank *obj, int account1, int account2, long long money) {
+ if (!valid(obj, account1) || !valid(obj, account2) ||
+ obj->balance[account1 - 1] < money)
+ return false;
+ obj->balance[account1 - 1] -= money;
+ obj->balance[account2 - 1] += money;
+ return true;
+}
+
+bool bankDeposit(Bank *obj, int account, long long money) {
+ if (!valid(obj, account))
+ return false;
+ obj->balance[account - 1] += money;
+ return true;
+}
+
+bool bankWithdraw(Bank *obj, int account, long long money) {
+ if (!valid(obj, account) || obj->balance[account - 1] < money)
+ return false;
+ obj->balance[account - 1] -= money;
+ return true;
+}
+
+void bankFree(Bank *obj) {
+ free(obj->balance);
+ free(obj);
+}
+
+int main() {
+ long long balance[] = {10, 100, 20, 50, 30};
+ Bank *obj = bankCreate(balance, ARRAY_SIZE(balance));
+ printf("%d\n", bankWithdraw(obj, 3, 10)); // expext: 1
+ printf("%d\n", bankTransfer(obj, 5, 1, 20)); // expect: 1
+ printf("%d\n", bankDeposit(obj, 5, 20)); // expect: 1
+ printf("%d\n", bankTransfer(obj, 3, 4, 15)); // expect: 0
+ printf("%d\n", bankWithdraw(obj, 10, 50)); // expect: 0
+ bankFree(obj);
+}
diff --git a/simple_bank_system.py b/simple_bank_system.py
@@ -0,0 +1,58 @@
+# 2043. Simple Bank System
+from collections import Counter
+
+"""
+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 'n' accounts numbered from 1 to 'n'. the initial balance of each
+account is stored in a 0-indexed integer array 'balance' with the '(i + 1)'th
+account having an initial balance of 'balance[i]'.
+"""
+
+
+class Bank(object):
+ def __init__(self, balance):
+ """
+ :type balance: List[int]
+ """
+ self.balance = Counter(dict(enumerate([0] + balance)))
+
+ def transfer(self, account1, account2, money):
+ """
+ :type account1: int
+ :type account2: int
+ :type money: int
+ :rtype: bool
+ """
+ return self.withdraw(account1, money) and (
+ self.deposit(account2, money) or not self.deposit(account1, money)
+ )
+
+ def deposit(self, account, money):
+ """
+ :type account: int
+ :type money: int
+ :rtype: bool
+ """
+ return account < len(self.balance) and not self.balance.update({account: money})
+
+ def withdraw(self, account, money):
+ """
+ :type account: int
+ :type money: int
+ :rtype: bool
+ """
+ return (
+ account < len(self.balance)
+ and self.balance[account] >= money
+ and not self.balance.update({account: -money})
+ )
+
+
+if __name__ == "__main__":
+ obj = Bank(balance=[10, 100, 20, 50, 30])
+ print(obj.withdraw(3, 10))
+ print(obj.transfer(5, 1, 20))
+ print(obj.deposit(5, 20))
+ print(obj.transfer(3, 4, 15))
+ print(obj.withdraw(10, 50))