코딩 알고리즘 문제/Leetcode

2043. Simple Bank System (Array, Hash Table, Design, Simulation)

highlightmoon 2025. 10. 23. 09:36
반응형

링크 - https://leetcode.com/problems/simple-bank-system/description/?envType=company&envId=facebook&favoriteSlug=facebook-thirty-days

난이도 - Medium

Intuition

 

Code

class Bank:

    def __init__(self, balance: List[int]):
        self.balance = balance

    def transfer(self, account1: int, account2: int, money: int) -> bool:
        if not self.validateAccount(account1) or not self.validateAccount(account2):
            return False

        if self.balance[account1-1] < money:
            return False

        self.balance[account1-1] -= money
        self.balance[account2-1] += money
        return True

    def deposit(self, account: int, money: int) -> bool:
        if not self.validateAccount(account):
            return False

        self.balance[account-1] += money
        return True
        

    def withdraw(self, account: int, money: int) -> bool:
        if not self.validateAccount(account):
            return False
            
        if self.balance[account-1] < money:
            return False

        self.balance[account-1] -= money
        return True

    def validateAccount(self, account):
        if 1 <= account <= len(self.balance) + 1:
            return True
        return False
        


# Your Bank object will be instantiated and called as such:
# obj = Bank(balance)
# param_1 = obj.transfer(account1,account2,money)
# param_2 = obj.deposit(account,money)
# param_3 = obj.withdraw(account,money)

Complexity

Time Complexity: O(1)

Space Complexity: O(1) (O(n) on initializing)

반응형