Leetcode: Sequence Reconstruction

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是不行的
 1 public class Solution {
 2     public boolean sequenceReconstruction(int[] org, int[][] seqs) {
 3         HashMap<Integer, HashSet<Integer>> graph = new HashMap<>();
 4         HashMap<Integer, Integer> 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<Integer>());
11                     indegree.put(seq[0], 0);
12                 }
13             }
14             else {
15                 for (int i=0; i<seq.length-1; i++) {
16                     if (!graph.containsKey(seq[i])) {
17                         graph.put(seq[i], new HashSet<Integer>());
18                         indegree.put(seq[i], 0);
19                     }
20                     if (!graph.containsKey(seq[i+1])) {
21                         graph.put(seq[i+1], new HashSet<Integer>());
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<Integer> queue = new LinkedList<>();
34         for (Map.Entry<Integer, Integer> 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 sequence
41         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<Integer> 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 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值