15% off all plans & roadmaps Use code
See plansLeetCode 444 · Solution & editorial
You are given an integer array org of length n, which is a permutation of the integers from 1 to n. You are also given a list of sequences seqs, where each seqs[i] is a subsequence of org.
Sequence Reconstruction is a Graph Traversal Patterns (DFS & BFS) problem, in the Graph BFS - Topological Sort (Kahn's Algorithm) subpattern. The pattern covers DFS/BFS traversal, topological sort, shortest paths, Union-Find, and MST
Master topological sorting using Kahn's BFS algorithm. Solve course schedule and build order problems.
The reference solution runs in O(n * s) time and O(n) space.
Sequence Reconstruction has been reported in interviews at Google.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Sequence Reconstruction is a Graph Traversal Patterns (DFS & BFS) problem, in the Graph BFS - Topological Sort (Kahn's Algorithm) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.