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
The bank state is just a list of balances, where account k maps to index k - 1. Every operation first validates that the account number is in range with is_valid_account, then mutates the corresponding balance.
The edge cases that drive the design are: an invalid account number (any operation returns False), an insufficient balance on withdraw (the withdraw fails without changing state), and a failed transfer. When a transfer withdraws from the source but the deposit to the target fails, the withdrawn money is re-deposited back into the source account so no money is lost.
- Time
- O(1)
is_valid_account,deposit, andwithdraweach do a single bounds check plus one balance update -O(1).transfercallswithdrawanddeposit(and possibly a rollbackdeposit), a fixed number ofO(1)calls.- Space
- O(n)
self.balancestores one entry per account, wherenis the total number of accounts passed to__init__.
1603. Design Parking System
A carType can be of three kinds: big, medium, or small, represented by 1, 2, and 3 respectively. By using an array, we can make use of those indices to retrieve the remaining capacity for each car type directly.
The list is built as [None, big, medium, small] so that availability[carType] lines up with the car type without any offset arithmetic. Adding a car succeeds only when that slot still has remaining capacity, decrementing it on success.
- Time
- O(1)
__init__()takes O(1)addCar()takes O(1)- Space
- O(1)
- A fixed-size array of four slots is used regardless of input.
1476. Subrectangle Queries
The straightforward solution: on every updateSubrectangle() you write newValue into each cell of the given subrectangle, and on getValue() you simply return the stored cell. The matrix always holds the true current state, so reads are trivial but every update touches the whole subrectangle.
- Time
- O(m*n)
__init__()is O(1) (just stores the reference).updateSubrectangle()walks every cell of the given subrectangle viaiterate_over, up toO(m*n)in the worst case.getValue()is O(1), a direct lookup.- Overall bounded by the worst-case update: O(m*n), where
mis the number of rows andnis the number of columns. - Space
- O(m*n)
self.rectanglestores the full grid, O(m*n).
1656. Design an Ordered Stream
The problem asks us to return the longest sorted available slice of the list (a chunk) during each insertion. If there is no chunk available, we return [].
We keep the values in a backing array indexed by idKey - 1, and track the longest contiguous filled prefix from index 0 using a pointer. After inserting a value, we advance the pointer past every consecutive filled slot, then return the slice between the inserted key and the new pointer position - which is exactly the newly available chunk.
- Time
- O(n)
__init__()takes O(n)insert()takes O(k)- n is the length of the array
- k is the length of the chunk to be returned
- Space
- O(n)
- The backing array of size n is stored.
where:
271. Encode and Decode Strings
Pick a delimiter character that can never appear inside any input string, then join on encode and split on decode. Here 'π' stands in for that delimiter, so it only works when the inputs are guaranteed not to contain it - a length-prefix encoding is needed for arbitrary byte strings.
- Time
- O(n)
encode()joins all strings in a single pass over their combined lengthndecode()splits the joined string in a single pass overn- Space
- O(n)
- The encoded string and the decoded list both hold all the original characters