One question a day to ensure a sharp mind.
L0
: straight forward questionL1
: variance of templateL2
: need to think for a while / complex implementationL3
: need aha moment / unexpected algorithm
Row: input size(IS), column: time complexity(TC)
IS&TC | O( |
O( |
O( |
O( |
O(nlogn) | O(n) | O(logn) | O(1) |
---|---|---|---|---|---|---|---|---|
1-10 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
10-50 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
50-100 | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
100-500 | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
500 - |
✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
✗ | ✗ | ✗ | ? | ✓ | ✓ | ✓ | ✓ |
|
✗ | ✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ |
|
✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ | ✓ |
TC | Algorithm |
---|---|
O( |
DFS-combination( |
O( |
DP |
O( |
DP, Floyd-Warshall |
O( |
DP |
O(nlogn) | Sorting, Heap, divide&conquer, Dijkstra-heap, QuickSort |
O(n) | DP, DFS-tree(V), BFS(V+E), TopologicalSorting(V+E), BucketSort(N+K), MonotonicStack() |
O(logn) | BinarySearch, BinaryIndexTree |
O(1) | Math |
- What is the data size?
- Can I sort or group the elements?
- Can I use DP, greedy or binary search?
- Can I enumerate on specific variable?
- Can I use two passes?
- Can I solve it reversely?
- Can I convert the problem to a other problem?
- If the problem mentions "Subarray", consider sliding window, monotonic stack/queue, prefix sum + hash table, Kadane's algorithm.
Efficiently find all the palindrome numbers in a range 10**9:
pal = []
base = 1
while base <= 10000: #
# odd number
for i in range(base, base * 10):
x = i
t = i // 10
while t:
x = x * 10 + t % 10
t //= 10
pal.append(x)
# even number
if base <= 1000:
for i in range(base, base * 10):
x = t = i
while t:
x = x * 10 + t % 10
t //= 10
pal.append(x)
base *= 10
pal.append(1_000_000_001) # sentinel