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] [[1],[2,3],[3,2]]

Output:true

Expected:false

How to revise: check if index at last equals graph‘s size

 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;
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;
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 }
时间: 2024-12-17 22:34:28

Leetcode: Sequence Reconstruction的相关文章

[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 se

444.&#160;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 se

[LeetCode] Queue Reconstruction by Height 根据高度重建队列

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers(h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h

LeetCode Queue Reconstruction by Height

原题链接在这里:https://leetcode.com/problems/queue-reconstruction-by-height/description/ 题目: Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is th

Sequence Reconstruction

Only Path :判断是否只存在一个拓扑排序的序列 只需要保证队列中一直最多只有1个元素即可 tricky part:1) org has all the nodes. That is why each time there should only be 1 element in the queue. If it is not, one node is missing. The normal Topology just checked whether all the node could b

LeetCode编程训练 - 拓扑排序(Topological Sort)

拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序(被依赖的排在前面),或给出排序结果. 最常用解决拓扑排序问题的方法是Kahn算法,步骤可以概括为: 1. 根据依赖关系,构建邻接矩阵或邻接表.入度数组 2. 取入度为0的数据(即不依赖其他数据的数据),根据邻接矩阵/邻接表依次减小依赖其的数据的入度 3. 判断减小后是否有新的入度为0的数据,继续进

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.