[Leetcode] 2043. Simple Bank System

RexiaN·2025년 10월 26일

n 명의 유저의 잔고가 들어있는 배열이 주어진다. 이후 송금, 예금, 인출 등의 작업에 대해 메서드를 작성하는 작업. 문제 그대로 Simple 해서 별다른 작업을 할 일이 없었다. 배열의 크기가 늘어나거나 줄어드는 일도 없어 시간 복잡도도 고려할 필요없었다. 손쉽게 패스.

class Bank {
    balances = []
    constructor(balance: number[]) {
        this.balances = balance
    }

    transfer(account1: number, account2: number, money: number): boolean {
        if (this.balances[account1 - 1] == null || this.balances[account2 - 1] == null) {
            return false
        }

        if (this.balances[account1 - 1] < money) {
            return false
        }

        this.balances[account1 - 1] -= money 
        this.balances[account2 - 1] += money

        return true
    }

    deposit(account: number, money: number): boolean {
        if (this.balances[account - 1] == null) {
            return false
        }

        this.balances[account - 1] += money
        
        return true
    }

    withdraw(account: number, money: number): boolean {
        if (this.balances[account - 1] == null || this.balances[account - 1] < money) {
            return false
        }

        this.balances[account - 1] -= money
        
        return true
    }
}

profile
Don't forget Rule No.1

0개의 댓글