博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Sequence Reconstruction
阅读量:5153 次
发布时间:2019-06-13

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

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:falseExplanation:[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:falseExplanation:The reconstructed sequence can only be [1,2].Example 3:Input:org: [1,2,3], seqs: [[1,2],[1,3],[2,3]]Output:trueExplanation: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是不行的
1 public class Solution { 2     public boolean sequenceReconstruction(int[] org, int[][] seqs) { 3         HashMap
> graph = new HashMap<>(); 4 HashMap
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
());11 indegree.put(seq[0], 0);12 }13 }14 else {15 for (int i=0; i
());18 indegree.put(seq[i], 0);19 }20 if (!graph.containsKey(seq[i+1])) {21 graph.put(seq[i+1], new HashSet
());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
queue = new LinkedList<>();34 for (Map.Entry
entry : indegree.entrySet()) {35 if (entry.getValue() == 0) {36 queue.offer(entry.getKey());37 }38 }39 40 int index = 0; //the index of the constructed topological sequence41 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; //since only one topological sequence exist, it should be org, check if current poll equals org[index]46 HashSet
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 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6201273.html

你可能感兴趣的文章
写接口请求类型为get或post的时,参数定义的几种方式,如何用注解(原创)--雷锋...
查看>>
【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
查看>>
Java网络编程--socket服务器端与客户端讲解
查看>>
List_统计输入数值的各种值
查看>>
学习笔记-KMP算法
查看>>
Timer-triggered memory-to-memory DMA transfer demonstrator
查看>>
跨域问题整理
查看>>
[Linux]文件浏览
查看>>
64位主机64位oracle下装32位客户端ODAC(NFPACS版)
查看>>
获取国内随机IP的函数
查看>>
今天第一次写博客
查看>>
江城子·己亥年戊辰月丁丑日话凄凉
查看>>
IP V4 和 IP V6 初识
查看>>
Spring Mvc模式下Jquery Ajax 与后台交互操作
查看>>
(转)matlab练习程序(HOG方向梯度直方图)
查看>>
『Raid 平面最近点对』
查看>>
【ADO.NET基础-数据加密】第一篇(加密解密篇)
查看>>
C语言基础小结(一)
查看>>
STL中的优先级队列priority_queue
查看>>
UE4 使用UGM制作血条
查看>>