Leetcode: Sequence Reconstruction

时间:2023-11-21 22:01:50
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 104. Reconstruction means building a shortest common supersequence of the sequences in seqs (i.e., a shortest sequence so that all sequences in seqs are subsequences of it). Determine whether there is only one sequence that can be reconstructed from seqs and it is the org sequence.

Example 1:

Input:
org: [1,2,3], seqs: [[1,2],[1,3]] Output:
false Explanation:
[1,2,3] is not the only one sequence that can be reconstructed, because [1,3,2] is also a valid sequence that can be reconstructed.
Example 2: Input:
org: [1,2,3], seqs: [[1,2]] Output:
false Explanation:
The reconstructed sequence can only be [1,2].
Example 3: Input:
org: [1,2,3], seqs: [[1,2],[1,3],[2,3]] Output:
true Explanation:
The sequences [1,2], [1,3], and [2,3] can uniquely reconstruct the original sequence [1,2,3].
Example 4: Input:
org: [4,1,5,2,6,3], seqs: [[5,2,6,3],[4,1,5,2]] Output:
true

Topological Sort: This problem is to determine if there's one, and only one sequence to sort a DAG. The method is to check if the queue's size is always 1 or not. If the queue has over 1 size when we're conducting topological sort, we return false, which implies that there exists more than 1 sequence to sort this DAG

Some corner case that i missed when write it:

Input:[1,2,3] [[1,2]]
Output:true
Expected:false
How to revise:  index==org.length? true : false;
Input:[1] [[1],[2,3],[3,2]]
Output:true
Expected:false
How to revise:  index==indegree.size()? true : false;
事实上,index==indegree.size()保证了这个DAG里面没有cycle, 有cycle就没有topological sequence存在,而我们这题要topological sequence存在且唯一,所以有cycle是不行的
 public class Solution {
public boolean sequenceReconstruction(int[] org, int[][] seqs) {
HashMap<Integer, HashSet<Integer>> graph = new HashMap<>();
HashMap<Integer, Integer> indegree = new HashMap<>(); //build the graph
for (int[] seq : seqs) {
if (seq.length == 1) {
if (!graph.containsKey(seq[0])) {
graph.put(seq[0], new HashSet<Integer>());
indegree.put(seq[0], 0);
}
}
else {
for (int i=0; i<seq.length-1; i++) {
if (!graph.containsKey(seq[i])) {
graph.put(seq[i], new HashSet<Integer>());
indegree.put(seq[i], 0);
}
if (!graph.containsKey(seq[i+1])) {
graph.put(seq[i+1], new HashSet<Integer>());
indegree.put(seq[i+1], 0);
}
if (!graph.get(seq[i]).contains(seq[i+1])) {
graph.get(seq[i]).add(seq[i+1]);
indegree.put(seq[i+1], indegree.get(seq[i+1])+1);
}
}
}
} //Topological sort, if any time the BFS queue's size > 1, return false;
Queue<Integer> queue = new LinkedList<>();
for (Map.Entry<Integer, Integer> entry : indegree.entrySet()) {
if (entry.getValue() == 0) {
queue.offer(entry.getKey());
}
} int index = 0; //the index of the constructed topological sequence
while (!queue.isEmpty()) {
int size = queue.size();
if (size > 1) return false;
int cur = queue.poll();
if (index>=org.length || org[index++] != cur) return false; //since only one topological sequence exist, it should be org, check if current poll equals org[index]
HashSet<Integer> neighbors = graph.get(cur);
for (int neighbor : neighbors) {
indegree.put(neighbor, indegree.get(neighbor)-1);
if (indegree.get(neighbor) == 0) {
queue.offer(neighbor);
}
}
}
return (index==org.length)&&(index==indegree.size())? true : false;
}
}