BackmediumUncategorized uncategorized medium

Minimum Path Distance in Undirected Graph Solution

Problem Statement

Given an integer n representing the number of nodes labeled from 0 to n-1, and a 2D integer array edges where edges[i] = [ui, vi] represents an undirected edge between nodes ui and vi, return the minimum number of edges to traverse to reach a target node from a starting node. If no path exists between the start and target, return -1.

Example 1
Input
n = 5, edges = [[0,1],[1,2],[2,3],[3,4]], start = 0, target = 4
Output
undefined

Explanation: The path from 0 to 4 is 0->1->2->3->4, which consists of 4 edges.

Constraints

  • 2 <= n <= 10^4
  • 0 <= edges.length <= 2 * 10^4
  • 0 <= start, target < n
  • start != target
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free