Sequence Reconstruction Problem
Description
LeetCode Problem 444.
You are given an integer array nums of length n where nums is a permutation of the integers in the range [1, n]. You are also given a 2D integer array sequences where sequences[i] is a subsequence of nums.
Check if nums is the shortest possible and the only supersequence. The shortest supersequence is a sequence with the shortest length and has all sequences[i] as subsequences. There could be multiple valid supersequences for the given array sequences.
- For example, for
sequences = [[1,2],[1,3]], there are two shortest supersequences,[1,2,3]and[1,3,2]. - While for
sequences = [[1,2],[1,3],[1,2,3]], the only shortest supersequence possible is[1,2,3].[1,2,3,4]is a possible supersequence but not the shortest.
Return true if nums is the only shortest supersequence for sequences, or false otherwise.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [1,2,3], sequences = [[1,2],[1,3]]
Output: false
Explanation: There are two possible supersequences: [1,2,3] and [1,3,2].
The sequence [1,2] is a subsequence of both: [1,2,3] and [1,3,2].
The sequence [1,3] is a subsequence of both: [1,2,3] and [1,3,2].
Since nums is not the only shortest supersequence, we return false.
Example 2:
Input: nums = [1,2,3], sequences = [[1,2]]
Output: false
Explanation: The shortest possible supersequence is [1,2].
The sequence [1,2] is a subsequence of it: [1,2].
Since nums is not the shortest supersequence, we return false.
Example 3:
Input: nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]]
Output: true
Explanation: The shortest possible supersequence is [1,2,3].
The sequence [1,2] is a subsequence of it: [1,2,3].
The sequence [1,3] is a subsequence of it: [1,2,3].
The sequence [2,3] is a subsequence of it: [1,2,3].
Since nums is the only shortest supersequence, we return true.
Constraints:
n == nums.length1 <= n <= 10^4numsis a permutation of all the integers in the range[1, n].1 <= sequences.length <= 10^41 <= sequences[i].length <= 10^41 <= sum(sequences[i].length) <= 10^51 <= sequences[i][j] <= n- All the arrays of
sequencesare unique. sequences[i]is a subsequence ofnums.
Solution
Approach: Topological Sort with a Single Choice
Each sequence fixes part of the order: if b comes right after a in some sequence, then a must come before b in every supersequence. Turn these facts into a directed graph with an edge a -> b for every adjacent pair in every sequence. A shortest supersequence uses each number once, so it is exactly an ordering of the numbers 1..n that respects all the edges, which is a topological order of the graph.
So the question becomes: does the graph have exactly one topological order? (If it does, that order must be nums. The problem guarantees every sequence is a subsequence of nums, so nums is always one valid order.)
Run Kahn's algorithm. Count each node's in-degree, and start with the nodes whose in-degree is 0, the numbers that could come first. At every step:
- If more than one node is ready, either could be placed next, so there are at least two orders. Stop and return
false. - If exactly one node is ready, it is forced. Remove it and decrease the in-degree of its neighbours; any that reach 0 become ready.
If the loop ends because nothing is ready any more, every number was forced one by one, and the order is unique: return true.
Example 1 shows the first case: after 1 is removed, both 2 and 3 are ready. Example 2 also fails, in a less obvious way. 3 never appears in a sequence, so it has in-degree 0 from the start next to 1. A number that no sequence mentions can never be forced into place.
Complexity: O(n + L) time and space, where L is the total length of all sequences (the number of edges is less than L).
class Solution:
def sequenceReconstruction(self, nums: List[int], sequences: List[List[int]]) -> bool:
n = len(nums)
graph = [[] for _ in range(n + 1)] # a -> b when b follows a directly in some sequence
indegree = [0] * (n + 1)
for seq in sequences:
for a, b in zip(seq, seq[1:]):
graph[a].append(b)
indegree[b] += 1
# Topological sort; the order is unique only if there is never more than one choice.
ready = [x for x in range(1, n + 1) if indegree[x] == 0]
while len(ready) == 1:
x = ready.pop()
for y in graph[x]:
indegree[y] -= 1
if indegree[y] == 0:
ready.append(y)
return len(ready) == 0