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-$10^3$ | ✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
✗ | ✗ | ✗ | ? | ✓ | ✓ | ✓ | ✓ |
|
✗ | ✗ | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ |
|
✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ | ✓ |
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 |
[a if cond1 else cond2 for a in A]
:if-else
statement infor
loopA[::i]
: jump i-steps in a list, e.g., odd index elementsA[::2]
and even index elementsA[1::2]
A.insert(0, n)
: insert item to list by a specific indexarr.sort(key=lambda x: (cond1, cond2, ..))
: sort array by multiple conditions. Condition can belen(x)
,x
#720sorted(list, key=functools.cmp_to_key(lambda x, y: int(y+x)-int(x+y)))
: custom compare function to a list
str.startswith(s)
: returns True if the string starts with the specified value, otherwise False
s.add(x)
: add value x to set s. Time: O(1)s1.update(s2)
: add set s2 to set s1. Time: O(len(s2))
Counter.most_common(num)
: return a list contains tuple.del Counter[key]
: delete an item. orCounter[key]=0
.cntA+cntB
: add two counters together.cntA-cntB
: subtract (keeping only positive counts).cntA&cntB
: find intersection of two counters. min(cntA, cntB)cntA|cntB
: find union of two counters. max(cntA, cntB)
heapq.heappush(heap, item)
: Push the value item onto the heap, maintaining the heap invariant.heapq.heappop(heap)
: Pop and return the smallest item from the heap, maintaining the heap invariant. If the heap is empty, IndexError is raised. To access the smallest item without popping it, use heap[0].heapq.heapify(x)
: Transform list x into a heap, in-place, in linear time.heapq.nlargest/nsmallest(n, iterable[, key])
: Return a list with the n largest elements from the dataset defined by iterable.