Skip to content

Latest commit

 

History

History
267 lines (233 loc) · 14 KB

File metadata and controls

267 lines (233 loc) · 14 KB

LeetCode skills

If you want to solve problems in the most understandable way, please look for Coding5DotCom.

Array

  • Array is consecutive in memory.
  • Cannot delete an item. Actually, it is overwrite. Delete a item of array will call the latter items move 1 to left. So it is O(n) time complexity.
  • C++ 2D array is also consecutive. But Java is not.

Hash function

You want to store students' information into a hash table. You want to query information by a student's name.

  • index = theHashFunction(student_name) the_information = the_hash_table[index].

Binary tree unified stack iteration

boolean mark solution.

Other Algorithms

Recursion

  • Recursion steps:
    1. Determine the parameters
    2. Determine the recursion logic
    3. Determine the return value
    4. Determine the exit logic

Dynamic programming

The principle of dynamic programming is from top to bottom, from left to right, from less to more, from near to far, from known to unknown and take turns being the boss.

Monotonic stack

  • Push indies one by one.
  • Only the useful indies are kept in the stack. Useless indices are popped (or eaten by followed larger (or smaller) index).

Graph theory

The principle of traversal (DFS or BFS) between undirected graph or directed graph are similar.

  • First rule: don't visited nodes again. This rule make starting from a node to traverse the undirected graph have a direction.

  • The adjacent nodes of undirected graph are its non-visited neighbors.

  • The adjacent nodes of directed graph are its targeted nodes.

  • Truth: An undirected graph can be understood as a bidirectional directed graph. Sometimes, we make use of it.

Minimum spanning a tree

  • Prim's algorithm can be used to minimum spanning a tree. It added the closest node to the tree each time. It uses a min_distances. It is recommended to use a priority_queue.
  • Kruskal's algorithm can also be used to minimum spanning a tree, but it adds the shortest edge each time. To combine the two nodes of an edge, UnionFind is used.

Shortest path

  • This is graph, not a tree. It can have cycles and many connected components.
  • Dijkstra's algorithm finds the shortest path from one vertex to all other vertices. It is like Prim's algorithm, also uses a min_distances, but the distance is to the original source vertex. All the weights of edges must not be a negative value.
  • Bellman_Ford algorithm finds the shortest path from one vertex to all other vertices. It effectively works in the cases of negative edges and is able to detect negative weight cycles. It also uses min_distances. Relaxation works by continuously shortening the calculated distance. It's straightforward and easily to be coded. The improved way with a queue is commonly more efficient. Relaxing All Edges by vertices.length – 1 times gives us Single Source Shortest Path to all vertices.
  • Bellman_Ford algorithm need to start from one source vertex each time and find the shortest paths to the source vertex. Floyd–Warshall algorithm can find all vertices' shortest paths.
  • What Floyd–Warshall algorithm solves can also be done by iterating through vertices and apply Bellman_Ford algorithm on each vertex; But if it is a Dense Graph, Floyd–Warshall algorithm is faster.
  • If all edges' weights are not negative, what Floyd–Warshall algorithm solves can also be done by iterating through vertices and apply Dijkstra algorithm on each vertex.
  • The time complexity of running V times Dijkstra algorithm is E * logE * V.
  • The time complexity of Floyd–Warshall algorithm is V * V * V. For a dense graph, Floyd–Warshall algorithm is still faster.
  • A* algorithm use a priority queue, pop() to get the vertex closest to the destination vertex. We need to choose proper math formula to determine which one is the closest. We to the very near place of destination vertex, we can use some special method to make it can handle the last part.

|Algorithm name|Focus|Key implementation methods|mark visited| |Prim's algorithm|Vertices|| |Kruskal's algorithm|Edges|Union-Find| |Dijkstra's algorithm|Vertices|| |Bellman-Ford algorithm|Edges(Vertices+Edges for SPFA)|| |Dijkstra's by heap sort - min_distance = A*| UnionFind + Heap sort = Kruskal BFS + heap sort = A*

Add a table to show the differences between A-Start and breadth-first search

Others

  • Find all the prime numbers within 1000000.

Solutions which need a perfection

Skipped problems/solutions

Binary Tree

  • Remember to add the recursion steps (described above in this doc) first

Backtracking

Greedy Algorithm

Dynamic programming

backpack problems

palindrome issue key: from middle, 2-d, +1 or +2, dp.size = len(s), do it on left-bottom side.

Graph

Failed in 2 rounds

other finished problems

Other algorithm

https://leetcode.cn/problems/closest-equal-element-queries

Timeout1

class Solution:

def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:

n = len(nums)

answer = []

for i in range(len(queries)):

index = queries[i]

num = nums[index]

k = 1

while k < n:

if num == nums[(index + k) % n]:

answer.append(k)

break

if num == nums[index - k]:

answer.append(k)

break

k += 1

if k == n:

answer.append(-1)

return answer