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:falseExplanation:[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:falseExplanation:The reconstructed sequence can only be [1,2].Example 3:Input:org: [1,2,3], seqs: [[1,2],[1,3],[2,3]]Output:trueExplanation: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是不行的
1 public class Solution { 2 public boolean sequenceReconstruction(int[] org, int[][] seqs) { 3 HashMap> graph = new HashMap<>(); 4 HashMap indegree = new HashMap<>(); 5 6 //build the graph 7 for (int[] seq : seqs) { 8 if (seq.length == 1) { 9 if (!graph.containsKey(seq[0])) {10 graph.put(seq[0], new HashSet ());11 indegree.put(seq[0], 0);12 }13 }14 else {15 for (int i=0; i ());18 indegree.put(seq[i], 0);19 }20 if (!graph.containsKey(seq[i+1])) {21 graph.put(seq[i+1], new HashSet ());22 indegree.put(seq[i+1], 0);23 }24 if (!graph.get(seq[i]).contains(seq[i+1])) {25 graph.get(seq[i]).add(seq[i+1]);26 indegree.put(seq[i+1], indegree.get(seq[i+1])+1);27 }28 }29 }30 }31 32 //Topological sort, if any time the BFS queue's size > 1, return false; 33 Queue queue = new LinkedList<>();34 for (Map.Entry entry : indegree.entrySet()) {35 if (entry.getValue() == 0) {36 queue.offer(entry.getKey());37 }38 }39 40 int index = 0; //the index of the constructed topological sequence41 while (!queue.isEmpty()) {42 int size = queue.size();43 if (size > 1) return false;44 int cur = queue.poll();45 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]46 HashSet neighbors = graph.get(cur);47 for (int neighbor : neighbors) {48 indegree.put(neighbor, indegree.get(neighbor)-1);49 if (indegree.get(neighbor) == 0) {50 queue.offer(neighbor);51 }52 }53 }54 return (index==org.length)&&(index==indegree.size())? true : false;55 }56 }