Tricky Time Complexity
list
List is like array so operation at beginning of the list
is costly.
append
:insert(0, item)
:pop
:pop(0)
:
deque
Because deque
at its core is a double-linked list so it's more efficient than list
appendleft
:append
:pop
:popleft
:in
: — since it's a linked list
Heap Theory
findMin()
:- In python we just query
heap[0]
- In python we just query
pop()
:- When calling
pop()
we need to start building the heap again to determine the lowest
- When calling
insert()
:
set
in
:x.union(y)
:x.intersect(y)
:
Sorted Set
in
:add
:remove
:
BFS
- with
- number of vertices — which is the same as the number of nodes.
- So given an array, would be
- number of edges — which is the connection between a node
- For example a 3x2 array would have
7
. - We can use the following formula:
- Which in this case
- For example a 3x2 array would have
- number of vertices — which is the same as the number of nodes.
DFS recursion
- Depends on how we call the recursion in the
for dr,dc
loop.- If there is 4 and the total node we travel is
m x n
then it would be - If there is 2 direction and the total node we travel is
m + n
then it would be
- If there is 4 and the total node we travel is
Trie
- Add
w
words of sizel
: - Search for word of size
l
: - Prefix
Skip List
- Search:
- Insert:
- Delete:
Permutation
- Time compexity: since the tree expands out from
n
items at the root and thenn-1, n-2, n-3,...
and at each pivot (start index) we have ways of choosing
Combination Sum
- Time complexity: since at each number we can either take or not (2 choices), and then we can choose duplication so sum up to a target
Subset
- Time complexity: for number we will have subsets. And we perform the
basket.copy()
which will be