图的题目一直是pain in the ass,虽然总体来说只有两个算法来解决这个问题:DFS和BFS 常用的数据结构有:hashmap
此外 要注意某一些能用并查集去做的题目。
而且也要注意那些看上去并不属于图的题目 也很有可能可以用图去做。
由于图的题目变化多端,所以如何对图的题目进行整理分类?
大多数题目,用BFS也可以 用DFS也可以,只是有的用DFS好写一些 有的用BFS好写一些
DFS也好 BFS也好 都是通过遍历所有的节点来得到我们想要得到的东西。
所以图的题目就很简单了?无非是DFS BFS?
当然不是。图的问题 难点在于最开始的时候如何读懂那一大长串题目 并如何用DFS/BFS来解决问题(有个清晰的思路 这个是最难的)
而且在实现过程中 会出现许许多多的corner case, 让人一度崩溃到绝望。
代码很长,但是一定要注意分块标注此块功能 避免忘记 这样debug的时候也好办。
下面的问题 虽然不都是高频题目 但是归类非常好
-
- Satisfiability of Equality Equations - union find
-
- Find the Town Judge
-
- Clone Graph
-
- Find the City With the Smallest Number of Neighbors at a Threshold Distance
-
- Count Servers that Communicate
-
- Possible Bipartition
-
- Course Schedule
-
- Course Schedule IV
-
- Reorder Routes to Make All Paths Lead to the City Zero
-
- Minimum Moves to Reach Target with Rotations
-
- Smallest String With Swaps
-
- Cheapest Flights Within K Stops
-
- Reconstruct Itinerary
-
- Number of Nodes in the Sub-Tree With the Same Label
-
- Generate Random Point in a Circle
-
- Detect Cycles in 2D Grid
-
- Minimum Height Trees
-
- Word Ladder
-
- Friend Circles
-
- Min Cost to Connect All Points
-
- Redundant Connection
-
- Redundant Connection II
-
- Network Delay Time
-
- Accounts Merge
-
- Loud and Rich
-
- Evaluate Division
-
- Count Subtrees With Max Distance Between Cities
-
- Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
-
- Shortest Path in a Grid with Obstacles Elimination
-
- Maximum Candies You Can Get from Boxes
-
- Jump Game IV
-
- Check if There is a Valid Path in a Grid
-
- As Far from Land as Possible
-
- Shortest Path in Binary Matrix
-
- Coloring A Border
-
- Number of Enclaves
-
- Max Area of Island
-
- Rotting Oranges
-
- Flood Fill
-
- Lexicographically Smallest String After Applying Operations
-
- Graph Connectivity With Threshold
-
- Most Stones Removed with Same Row or Column
-
- Minimum Jumps to Reach Home