Does One Skill Lead to Another?
Easy · Topological Sort & Shortest Paths · about 15 min
A learning app has n skills, numbered 0 to n - 1. Each [a, b] in links means skill a directly unlocks skill b. Skill x leads to skill y if you can go from x to y by following one or more links. The links never form a loop, so a skill never leads to itself.
For each question [x, y] in queries, answer whether x leads to y. Return the answers as a list of booleans in the same order.
1 <= n <= 100, up to 10,000 questions.
Example: skill_chain_answers(4, [[0, 1], [1, 2], [2, 3]], [[0, 3], [3, 0], [1, 2]]) → [True, False, True]
Hints
- There are many questions but few skills. Can you work out every pair once, up front?
- x leads to y if x leads to some k and k leads to y. Try every skill as the middle step k.
- Put the loop over the middle step on the outside, then answer each question by looking it up.
Learn the pattern first: the Topological Sort & Shortest Paths lesson.
More Topological Sort & Shortest Paths problems
- Finish Every Training Module (Easy)
- Alarm Across the Campus (Easy)
- Flip the Fewest One-Way Signs (Easy)
- Letters of a Made-Up Language (Medium)
- Fewest Terms to Graduate (Medium)
- Budget Trip With Few Layovers (Medium)
- The Gentlest Hike (Medium)
- Wire Up the Sensors (Medium)
- Pick the Quietest Depot (Medium)
- Rebuild the Travel Route (Medium)
- Cross Before the Flood (Hard)
- How Many Fastest Routes? (Hard)
- Earliest Launch Day (Hard)
- Toll Roads With Coupons (Hard)