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 ...
随机推荐
- 分布式缓存技术memcached学习(五)—— memcached java客户端的使用
Memcached的客户端简介 我们已经知道,memcached是一套分布式的缓存系统,memcached的服务端只是缓存数据的地方,并不能实现分布式,而memcached的客户端才是实现分布式的地方 ...
- java-集合2
浏览以下内容前,请点击并阅读 声明 下面对集中核心集合的接口分别总结 Collection接口 一般情况下,集合的实现类会有一个含有Collection类型为参数的构造器,可以由一个指定的集合类创建该 ...
- Ext3文件系统及JDB介绍
Ext3介绍 对于ext3文件系统,磁盘空间划分一系列block groups,每个group有位图来跟踪inode和data块的分配和范围.其物理布局如下: Superblock:位于group内第 ...
- 【DP】POJ 2385
题意:又是Bessie 这头牛在折腾,这回他喜欢吃苹果,于是在两棵苹果树下等着接苹果,但苹果不能落地后再接,吃的时间不算,假设他能拿得下所有苹果,但是这头牛太懒了[POJ另一道题目说它是头勤奋的奶牛, ...
- ubuntu安装配置elasticSearch(vagrant)
安装jdk sudo apt-get install python-software-properties sudo add-apt-repository ppa:webupd8team/java s ...
- iOS宏和__attribute__
本文目录 iOS宏的经典用法 Apple的习惯 __attribute__ iOS宏的经典用法 1.常量宏.表达式宏 #define kTabBarH (49.0f) #define kScreenH ...
- Git 常用命令行
最近在公司的服务器上安装了Git Sever,开始从SVN转向到Git了,整理了一些在Git常用的命令. 取得Git仓库 初始化一个版本仓库 git initClone远程版本库 git clone ...
- ArrayList 实现删除重复元素(元素为对象类型)
package 集合; import java.util.ArrayList;import java.util.Iterator; /* * 删除集合中的重复的元素(元素是对象形式的) * * Li ...
- ListView使用item显示不同布局
/** * 自定义城市列表适配器 */ private class MyCityListAdapter extends BaseAdapter { final int VIEW_TYPE = 2; f ...
- UIPickerView理解