knaxuslearn · build · ship

week 11 of 15

Graphs I: traversal

Get comfortable with these coding mechanics first: building an adjacency list from an edge list, a visited set, a queue, a directions list [(1,0),(-1,0),(0,1),(0,-1)], bounds checks before indexing. Template to type from memory by the end of the week: grid or graph search: visited set, queue or recursion, loop over neighbours.

  1. 91Number of Islands (opens LeetCode in a new tab)Grid DFS or BFSMediumcore
    Common mistake
    Checking bounds after indexing instead of before.
  2. 92Clone Graph (opens LeetCode in a new tab)DFS with a mapMedium
    Common mistake
    Storing the copy in the map after recursing.
  3. 93Rotting Oranges (opens LeetCode in a new tab)Multi-source BFSMediumcore
    Common mistake
    Counting a minute for a level that did no work.
  4. 94Is Graph Bipartite? (opens LeetCode in a new tab)Colouring with BFS or DFSMedium
    Common mistake
    Disconnected graphs: start a search from every uncoloured node.
  5. 95Pacific Atlantic Water Flow (opens LeetCode in a new tab)Reverse DFS from the bordersMedium
    Common mistake
    Comparing in the wrong direction. Water flows uphill from the ocean, so use >=.
  6. 96Course Schedule (opens LeetCode in a new tab)Topological sort (Kahn's)Medium
    Common mistake
    Edge direction: [a, b] means b comes before a.
  7. 97Course Schedule II (opens LeetCode in a new tab)Topological orderMediumcore
    Common mistake
    Returning a partial order when there is a cycle.