The Count the Number of Houses at a Certain Distance I coding problem describes a street with houses arranged in a line. Additionally, a shortcut exists between two specific houses and . For every distance from 1 to , you need to find how many pairs of houses have a shortest path distance exactly equal to .
Note that the path can follow the linear street or use the shortcut.
Oracle uses this problem to evaluate a candidate's ability to model a scenario as a graph. It tests knowledge of the BFS interview pattern or the shortest path pattern. Since is typically small in version I of this problem, it evaluates whether you can correctly identify the "shortcut" edges and run a standard graph algorithm efficiently.
This problem is solved using Graph Construction and Breadth-First Search (BFS) (or the Floyd-Warshall algorithm).
dist[j]., .
(1,2), (2,3), (1,3) (the shortcut).Always visualize "shortcut" problems as adding a single edge to a regular graph (like a line or a ring). Most of the distances will still follow the original pattern, and only those "near" the shortcut will change. This intuition helps in moving from BFS to or mathematical solutions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count the Number of Houses at a Certain Distance II | Hard | Solve | |
| Shortest Path with Alternating Colors | Medium | Solve | |
| Shortest Cycle in a Graph | Hard | Solve | |
| Keys and Rooms | Medium | Solve | |
| Flower Planting With No Adjacent | Medium | Solve |