博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
444. Sequence Reconstruction
阅读量:2351 次
发布时间:2019-05-10

本文共 2777 字,大约阅读时间需要 9 分钟。

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 ≤ 10^4. 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

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

我的想法

甚至都不知道为什么这道题可以用BFS做。。。。其实当知道这个题是用BFS之后,有想到应该和Topological Sorting相关,但是没有理明白

解答

jiuzhang solution:

public class Solution {
public boolean sequenceReconstruction(int[] org, int[][] seqs) {
Map
> map = new HashMap
>(); Map
indegree = new HashMap
(); for (int num : org) {
map.put(num, new HashSet
()); indegree.put(num, 0); } //calculate indegree of nodes in each seq int n = org.length; int count = 0; for (int[] seq : seqs) {
count += seq.length; //org is a permutation of 1-n, so there is no possibility //that seq[i] could smaller than 1 or bigger than n if (seq.length >= 1 && (seq[0] <= 0 || seq[0] > n)) return false; for (int i = 1; i < seq.length; i++) {
if (seq[i] <= 0 || seq[i] > n) return false; //HashSet.add(e) will return true if the set did not //already contain e if (map.get(seq[i - 1]).add(seq[i])) indegree.put(seq[i], indegree.get(seq[i]) + 1); } } // case: [1], [] if (count < n) return false; //add 0 indegree nodes to the queue Queue
q = new ArrayDeque
(); for (int key : indegree.keySet()) if (indegree.get(key) == 0) q.add(key); //topological sorting int cnt = 0; while (q.size() == 1) { int ele = q.poll(); for (int next : map.get(ele)) { indegree.put(next, indegree.get(next) - 1); if (indegree.get(next) == 0) q.add(next); } if (ele != org[cnt]) { return false; } cnt++; } return cnt == org.length; }}

转载地址:http://xkqvb.baihongyu.com/

你可能感兴趣的文章
Tab Layout 之多个Activity实现
查看>>
FrameLayout之我见
查看>>
个人解读Activity之一
查看>>
实现自定义布局的Notification
查看>>
AlarmManager的学习与实现
查看>>
解读Content Provider之一
查看>>
解读Content Provider之二
查看>>
自定义UI实例
查看>>
推荐一个不错的自定义UI
查看>>
fedora16 设置 gedit软件的默认编码
查看>>
S3C6410 存储器映射
查看>>
Linux 3.3.0移植到S3C6410开发板上之一
查看>>
Busybox支持中文的解决办法
查看>>
Spring中BeanFactory和FactoryBean有什么区别?
查看>>
牛年(2021)的KPI
查看>>
快速识别图片类型
查看>>
理解云原生
查看>>
docker常见问题答疑
查看>>
mac最简配置maven
查看>>
虚拟机性能监控与故障处理工具
查看>>