CodeMosa

Master LeetCode Patterns

5 Problems

Graph Shortest Paths

BFS, Dijkstra, 0/1 BFS, and all-pairs approaches in practice.

Back to Collections

Relevant Patterns

#
Study these pattern guides before tackling the problems. Understanding the core concepts will help you solve them more efficiently.

Practice Problems

#
Show 2 hints
  • Use Dijkstra from the source; answer is max distance or -1 if unreachable.
  • Store adjacency as (to, weight); skip stale heap entries.
Show 2 hints
  • Binary search on answer + BFS, or Dijkstra on effort (max edge along path).
  • Grid neighbors with abs height diff as weight.
Show 2 hints
  • DP/Bellman-Ford variant on edges for up to K+1 layers.
  • Alternatively use BFS/priority with (node, cost, stops) and prune by best [node][stops].
Show 2 hints
  • 8-direction BFS from (0,0); mark visited; return steps when reaching (n-1,n-1).
  • Check starting/ending cell blocked cases.
Show 2 hints
  • Run Dijkstra from each node (or Floyd–Warshall) and count neighbors ≤ threshold.
  • Tie-break by city index descending if required.

Track Your Progress

Mark problems as complete as you solve them. Come back anytime to continue where you left off.