Big O - worst case Clasification - 3:41
- Search
- Singly O(n)
- Doubly O(n)
- Insert at head
- Singly O(1)
- Doubly O(1)
- Insert at tail
- Singly O(1)
- Doubly O(1)
- Remove at head
- Singly O(1)
- Doubly O(1)
- Remove at tail
- Singly O(n) : because need to find new tail
- Doubly O(1) : because tail have previous pointer which will become the new tail
- Remove at middle
- Singly O(n)
- Doubly O(n)
- while
- recursive
for i=0; i<n; i++
O(n)for i=0; i<n; i=i+2
O(n)for i=n; i>0; i=--
O(n)for i=0; i<n; i=i*2
O(log n) base 2for i=0; i<n; i=i*3
O(log n) base 3for i=0; i<n; i=i/2
O(log n) base 2
- Choice + decision
- Find the base case
- Find the small input for next call
- Logic to do task
- Normally ans on leaf node
- Three type of problem solve by recursion
- DP: (asking about optimal) answer on the root (recursion + memory)
- Backtracking: (all the combination) answer on path
- DnC: ?
Watch till 1.11
- Binary tree have at most two children
- Binary search tree: Left sub tree has smaller and right sub tree have larger elements
- examples
5
/ \
9 8
/ /\
1 7 10
/\
6 9
Above tree is not a BST as 9 is in the left subtree of 8
1
\
20
/
19
/
2
\
3
\
18
/
17
Above tree is BST as each node's right sub tree have larger element and left have smaller element
-
Heap will be visually represent via tree
-
Heap will be stored via array
100 /
19 36 /\ /
17 3 25 1 /\
2 7
[100, 19, 36, 17, 3,25, 1, 2, 7]
Above is max heap, where root node represent the highest value in the heap. Every child will be less then their parent node
- Heap in go require to implement container/heap interface
- container/heap have following func which need to implement
Len() int
on value receiverLess(i, j int) bool
on value receiverSwap(i, j int)
on value receiveri<j
for min heap andi>j
for max heapPush(x interface{})
on pointer receiverPop() interface{}
on pointer receiver
- heap need to init by
heap.Init()
- Push and pop will be done via
heap.Push
andheap.Pop
- Subset, combination, permutation all needs backtracking algorithm
- There are two class of problems
- Combination : Doesn't care about ordering ex. [1,2] and [2,1] are the same
- Permutation : Care about ordering ex "AB" and "BA" are different letters
- Basic intuition:
- Choice : its all the choice made during the path of a tree
- Choose : What are going to choose on each node
- Explore : Explore further based on above chosen
- Un-choose : Un-choose so that next time, new chose can be made
- Implement via recursion
- Basic tricks and types of problem
https://www.youtube.com/watch?v=zL4mjpYpRmc
https://leetcode.com/explore/learn/card/binary-search/
- Edge
- Node/Vertex
- Connected Graph
- Dis-connected Graph
- Directed and undirected graph
- Cyclic and Acyclic graph
- Storing connected graph
Implementation
- Adjacency matrix
- Adjacency list
Graph Traversal
- DFS (Recursive & Iterative)
- Adjacency Matrix
- Adjacency List
- BFS: Iterative
- Adjacency Matrix:
DSA\graph\iterative_traversal.go
- Adjacency List
- Adjacency Matrix:
Topological Sort
- Applicable on directed acyclic graph
- If graph have cycle, it can't be applicable
- It used where one item is dependent on another node. It used to find the ordering based on that.
- Order depend upon which node pick first.
- Sudo code
- Take a stack to store the order, visited array to track visited node
- for each node run dfs
- in dfs
- if node is already in visited; return
- Add node in visited
- for each node in adjacency list
- if node is not in visited, run dfs
- add node in stack (means during DFS, it reach to the node which doesn't have any adjacent node; means this node have no dependency)
- in dfs
Dynamic Programming
- https://www.youtube.com/watch?v=9k31KcQmS_U
- https://www.youtube.com/watch?v=8LusJS5-AGo&list=PLrmLmBdmIlpsHaNTPP_jHHDx_os9ItYXr
- https://www.topcoder.com/thrive/articles/Dynamic%20Programming:%20From%20Novice%20to%20Advanced
- https://www.youtube.com/watch?v=kvyShbFVaY8&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=3