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:
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的更多相关文章
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode]444. Sequence Reconstruction
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [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 ...
- LeetCode: Queue Reconstruction by Height
这题的关键点在于对数组的重排序方法,高度先由高到低排列不会影响第二个参数,因为list.add的方法在指定index后面插入,因此对于同高的人来说需要对第二个参数由低到高排,具体代码如下 public ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- LeetCode编程训练 - 拓扑排序(Topological Sort)
拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- [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 ...
随机推荐
- HTML5页面绘图
canvas 标签:获取画笔: 对象.getContext("2d")制定画笔Syte: context.fillStyle = "red"; context. ...
- php 冒泡排序 快速排序
$a=array('3','8','1','4','11','7'); print_r($a); $len = count($a); //从小到大 for($i=1;$i<$len;$i++) ...
- Java日期处理
日常工作中经常遇到关于日期的处理,下面把自个写好的Java代码段分享一下,也当做自个的一个备份,同时也欢迎交流,如若分享请注明出处,谢谢. 1.返回两个时间段之间的月份: /** * 返回任意两个月份 ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文深入 ...
- Cookie案例:简单登录界面中的应用
ServletDemo1.java package ztq.servlet.study; import java.io.IOException; import java.io.PrintWriter; ...
- php文件写入PHP_EOL与FILE_APPEND
PHP_EOL 换行符 unix系列用 \n windows系列用 \r\n mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 FILE_APPEND 用于文本追加 ...
- PyCharm2016.2专业版注册码
43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...
- PHP 检测变量是否为空
PHP 中以下值得计算结果为 false: 关键字 boolean false 整型 integer 0 浮点型 double 0.0 字符串 string "" 字符串 str ...
- Myeclipse java程序中运行图片无法加载并且乱码
Windows---prefrence--workspace--text file encoding亲测有效