Skip to main content

Array, Hash

These are "design a class" problems where the right internal state is a plain list or an index trick rather than anything fancy. Mapping account numbers, car types, or stream keys onto array indices keeps every operation simple and fast.

General

2043. Simple Bank System

Medium·

Solutions:
Visualize
class Bank:
def __init__(self, balance: List[int]):
self.balance = balance
 
def transfer(self, account1: int, account2: int, money: int) -> bool:
if self.withdraw(account1, money):
if self.deposit(account2, money):
return True
self.deposit(account1, money)
return False
 
def deposit(self, account: int, money: int) -> bool:
if self.is_valid_account(account):
self.balance[account - 1] += money
return True
return False
 
def withdraw(self, account: int, money: int) -> bool:
if self.is_valid_account(account) and self.balance[account - 1] >= money:
self.balance[account - 1] -= money
return True
return False
 
def is_valid_account(self, account: int) -> bool:
return 1 <= account <= len(self.balance)

1603. Design Parking System

Easy·

Solutions:
Visualize
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.availability = [None, big, medium, small]
 
def addCar(self, carType: int) -> bool:
if self.availability[carType] > 0:
self.availability[carType] -= 1
return True
return False

1476. Subrectangle Queries

Medium·

Solutions:
Visualize
class SubrectangleQueries:
def __init__(self, rectangle: List[List[int]]):
self.rectangle = rectangle
 
def iterate_over(
self, row1: int, col1: int, row2: int, col2: int
) -> Tuple[int, int]:
for r in range(row1, row2 + 1):
for c in range(col1, col2 + 1):
yield r, c
 
def updateSubrectangle(
self, row1: int, col1: int, row2: int, col2: int, newValue: int
) -> None:
for row, col in self.iterate_over(row1, col1, row2, col2):
self.rectangle[row][col] = newValue
 
def getValue(self, row: int, col: int) -> int:
if 0 <= row < len(self.rectangle) and 0 <= col < len(self.rectangle[0]):
return self.rectangle[row][col]

1656. Design an Ordered Stream

Easy·

Solutions:
Visualize
class OrderedStream:
def __init__(self, n: int):
self.pointer = 0
self.data = [None for i in range(n)]
self.n = n
 
def insert(self, idKey: int, value: str) -> List[str]:
idKey = idKey - 1 # 0-indexing
self.data[idKey] = value
while self.pointer < self.n and self.data[self.pointer]:
self.pointer += 1
return self.data[idKey : self.pointer]