Given a directed graph with `n` nodes, represented by a list of `edges`, and a list of `ordered_nodes`, find the length of the longest path that visits every node exactly once. Additionally, the nodes specified in `ordered_nodes` must appear in the path in the exact relative order as they appear in the `ordered_nodes` list. If no such path exists, return -1. The nodes are 0-indexed.
Explanation: The path 0 -> 1 -> 2 -> 3 -> 4 visits all nodes exactly once. Nodes 0, 2, and 4 appear in the specified order. The length of this path is 4.
Analyze constraints and compute optimal solutions step-by-step.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak