반응형
난이도 - 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)
반응형
'코딩 알고리즘 문제 > Leetcode' 카테고리의 다른 글
| 122. Best Time to Buy and Sell Stock II (Array, Dynamic Programming, Greedy) (0) | 2025.10.23 |
|---|---|
| 9. Palindrome Number (Math) (0) | 2025.10.23 |
| 303. Range Sum Query - Immutable (Array, Design, Prefix Sum) (0) | 2025.10.23 |
| 480. Sliding Window Median (Array, Hash Table, Sliding Window, Heap (Priority Queue)) (0) | 2025.10.23 |
| 1060. Missing Element in Sorted Array (Array, Binary Search) (0) | 2025.10.23 |