End of Year Sale: Use Coupon Code END2025 to Get Extra 25% Off.
00DAYS
:
00HOURS
:
00MINUTES
:
00SECONDS
Leetcode Problem 399. Evaluate Division
399. Evaluate Division
Leetcode Solutions
Approach: Path Search in Graph
Build a graph from the input equations, with nodes representing variables and directed edges representing division relationships with associated weights.
For each query, check if a path exists between the nodes. If not, return -1.0.
If a path exists, perform a depth-first search (DFS) or breadth-first search (BFS) to find the path and calculate the cumulative product of weights along the path.
Return the cumulative product as the answer to the query.