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;
}
}

Leetcode: Sequence Reconstruction的更多相关文章

  1. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  2. [LeetCode]444. Sequence Reconstruction

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  3. [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 ...

  4. LeetCode: Queue Reconstruction by Height

    这题的关键点在于对数组的重排序方法,高度先由高到低排列不会影响第二个参数,因为list.add的方法在指定index后面插入,因此对于同高的人来说需要对第二个参数由低到高排,具体代码如下 public ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  6. [LeetCode] Course Schedule II 课程清单之二

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

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

    拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. [LeetCode] 210. Course Schedule II 课程清单之二

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

随机推荐

  1. Daily Scrum Meeting ——FifthDay

    一.Daily Scrum Meeting照片 牛姐去工程师那边了,已经在群里给我汇报了.橙汁去北京参加ICPC了 二.Burndown Chart 渐入佳境了,最后一礼拜了 三.项目进展 1.实现注 ...

  2. [工作中的设计模式]迭代子模式Iterator

    一.模式解析 迭代子模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象 1.迭代子模式一般用于对集合框架的访问,常用的集合框架为lis ...

  3. Delphi基本类型--枚举 子界 集合 数组

    [plain] view plain copy <strong>根据枚举定义集合 </strong> TMyColor = (mcBlue, mcRed); TMyColorS ...

  4. 我的jQuery源码读后感

    (function(window, undefined) { // 构造jQuery对象 var jQuery = (function() { var jQuery = function(select ...

  5. c语言完成宽带拨号

    学校的网络每次开机都需要手动登陆,于是用c写了一个自动登陆的小程序... 程序功能超级简单...只是懒得每次都登陆... PS:代码功能具体没有测试...我自己用的是python #include & ...

  6. MySQL 事务

    MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成 ...

  7. C#组合查询小Demo

    namespace WindowsFormsApplication1 { public partial class Form1 : Form { string Sql = "select * ...

  8. Eclipse下使用GDT插件无法登陆GAE & GDT无法上传JAVA代码

    今天更新github主页的过程中,想使用GAE部署一个Java Web服务来更好的支持网站动态性(关键是利用了免费的GAE资源),结果遇到了2个大问题. 1.GDT插件无法登陆GAE账户 错误1:登陆 ...

  9. 一起来做webgame,《Javascript贪食蛇》

    2019-09-22更新: 使用canvas实现:https://github.com/onlyfu/SnakeSir-Javascript 以下为HTML4实现: 今天来个略有意思的,<贪食蛇 ...

  10. 关于Unity3D手机网游开发一些小看法

    它的知识技能和职责,我就不仔细说了,说细了有一点像招聘启示.他的主要职责虽然负责技术,但是也给产品决策和方向提供一些决策.他最主要的考核指标,就是经验很重要,最好主导或参与过一款网络游戏的开发.举个例 ...