LeetPattern
Resources
Sheet
- In order to visualize and practice the algorithm, I use google sheets to make drafts. You also can have access to it here.

Prerequisites

Resources
Dynamic Programming
Steps to Solve DP Problems
- Define the
dp
array and its meaning.
- Define the
dp
formula.
- Initialize the
dp
array.
- Determine the traversal direction.
- Derive the
dp
array.
C++ Containers
Operation |
Method |
Append |
v.push_back(x); |
Pop last element |
v.pop_back(); |
Insert at position |
v.insert(v.begin() + index, x); |
Remove at position |
v.erase(v.begin() + index); |
Clear all elements |
v.clear(); |
Get size |
v.size(); |
Access element |
v[i] or v.at(i); |
Sort |
sort(v.begin(), v.end()); |
Operation |
Method |
Append (back) |
d.push_back(x); |
Append (front) |
d.push_front(x); |
Pop last |
d.pop_back(); |
Pop front |
d.pop_front(); |
Access front |
d.front(); |
Access back |
d.back(); |
Operation |
Method |
Enqueue (push) |
q.push(x); |
Dequeue (pop) |
q.pop(); |
Get front element |
q.front(); |
Get back element |
q.back(); |
Check if empty |
q.empty(); |
Get size |
q.size(); |
- Priority Queue (
std::priority_queue
)
Operation |
Method |
Insert |
pq.push(x); |
Remove top |
pq.pop(); |
Get top element |
pq.top(); |
Check if empty |
pq.empty(); |
Get size |
pq.size(); |
Min heap |
priority_queue<int, vector<int>, greater<int>> pq; |
Operation |
Method |
Push |
s.push(x); |
Pop |
s.pop(); |
Get top element |
s.top(); |
Check if empty |
s.empty(); |
Get size |
s.size(); |
Operation |
Method |
Insert |
s.insert(x); |
Remove |
s.erase(x); |
Check existence |
s.count(x); |
Get size |
s.size(); |
Find element |
s.find(x); |
Get first element |
*s.begin(); |
Get last element |
*s.rbegin(); |
- Unordered Set (
std::unordered_set
)
- Similar to
std::set
, but uses hashing for faster lookups
Operation |
Method |
Insert |
us.insert(x); |
Remove |
us.erase(x); |
Check existence |
us.count(x); |
Find element |
us.find(x); |
Operation |
Method |
Insert/update |
mp[key] = value; |
Get value |
mp[key]; |
Remove key |
mp.erase(key); |
Check existence |
mp.count(key); |
Get size |
mp.size(); |
Iterate |
for (auto [k, v] : mp) { ... } |
- Unordered Map (
std::unordered_map
)
- Similar to
std::map
, but uses hashing for faster lookups
Operation |
Method |
Insert/update |
ump[key] = value; |
Get value |
ump[key]; |
Remove key |
ump.erase(key); |
Check existence |
ump.count(key); |
Operation |
Method |
Push front |
lst.push_front(x); |
Push back |
lst.push_back(x); |
Pop front |
lst.pop_front(); |
Pop back |
lst.pop_back(); |
Insert at position |
lst.insert(it, x); |
Remove element |
lst.remove(x); |