← 返回 amazon 的题目列表Longest Path in Directed Acyclic Graph
类型:online_judge
Given a Directed Acyclic Graph (DAG) represented as a list of directed edges, where each edge is a pair of strings indicating a connection from one node to another, determine the longest path in the graph. The output should be a list of node names representing the sequence of nodes along the longest path from the starting node to the ending node.
Constraints:
The input graph is a DAG (no cycles).
The number of edges is in the range [0, 104].
Node names are represented as uppercase strings with length between 1 and 5.
Example 1:
Input: edges = [["NYC","SFO"], ["SFO","SEA"], ["SFO","JFK"], ["PDX","SEA"]]
Output: ["NYC", "SFO", "SEA"] or ["NYC", "SFO", "JFK"]
Explanation:
The longest path starts at NYC, goes to SFO, and ends at SEA. ["NYC", "SFO", "JFK"] is also a valid answer.