loading

Evaluate Division — Step-by-Step Visualization

mediumLeetCode #399ArrayGraphUnion FindShortest Path

You are given equations like A/B=k and queries like A/B, C/D. Evaluate each query or return -1 if unknown.

Algorithm Pattern

Weighted Graph BFS

Key Idea

Build graph where each A→B edge has weight k and B→A has weight 1/k. BFS from source to target, multiplying edge weights.

Step-by-Step Approach

  1. Build adjacency list with weights
  2. For each query, BFS from source to destination
  3. Multiply weights along the path
  4. Return -1 if no path or node unknown

Related Problems