Sequence Reconstruction

Only Path :
判断是否只存在一个拓扑排序的序列 只需要保证队列中一直最多只有1个元素即可

tricky part:
1) org has all the nodes. That is why each time there should only be 1 element in the queue. If it is not, one node is missing. The normal Topology just checked whether all the node could be accessed. It doesn‘t mean one path contains all the node.

2) The seqs probably give the duplicates info for two edges, we need to check whether it is added to set successfully to determine whether we need to increase the degree.

3) The sequence in seqs might only have one element, we need to process it seperately.

4) For the cases like [1] [[][]], we need to be able to rule out them. The edges are stored in the seqs, the mininum seqs length is the same as org. So if org.length is more than the seq.length together in seqs, there are missing information about certain edges. We can directly return false.

public class Solution {
    /**
     * @param org a permutation of the integers from 1 to n
     * @param seqs a list of sequences
     * @return true if it can be reconstructed only one or false
     */
    public boolean sequenceReconstruction(int[] org, int[][] seqs) {
        HashMap<Integer, HashSet<Integer>> map = new HashMap<>();
        HashMap<Integer, Integer> indgree = new HashMap<>();

        for (int i : org) {
            map.put(i, new HashSet<Integer>());
            indgree.put(i, 0);
        }

        int n = org.length;
        int count = 0;
        for (int[] seq : seqs) {
            count += seq.length;
            //System.out.println(seq.length);
            if (seq.length == 1 && (seq[0] > n || seq[0] < 1)) {
                return false;
            }
            for (int s = 1; s < seq.length; s++) {
                if (seq[s] > n || seq[s] < 1) {
                    return false;
                }
                if (map.get(seq[s - 1]).add(seq[s])) { // this is need to check to avoid duplicate edge
                    indgree.put(seq[s], indgree.get(seq[s]) + 1);
                }
            }
        }

        if (count < n) {
            return false;
        }

        Queue<Integer> q = new LinkedList<>();
        for (int key : indgree.keySet()) {
            if (indgree.get(key) == 0) {
                q.offer(key);
            }
        }
        count = 0;
        while (q.size() == 1) {
             int num = q.poll();
             count++;
             for (int s : map.get(num)) { // don‘t need check, because of the initial
                 indgree.put(s, indgree.get(s) - 1);
                 if (indgree.get(s) == 0) {
                     q.offer(s);
                 }
             }
        }
        return count == n;

    }
}
时间: 2024-10-05 00:47:40

Sequence Reconstruction的相关文章

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 se

[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 se

444.&#160;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 se

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

google onsite 1

Given a 2D matrix , with obstacles. Start from any of the first row, exit from any of the last row. What is the minimum steps? 000001 000100 000000 000100 step 2. Given a list of buslines, and the stops of each busline, given a start stop, and a end

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

拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序(被依赖的排在前面),或给出排序结果. 最常用解决拓扑排序问题的方法是Kahn算法,步骤可以概括为: 1. 根据依赖关系,构建邻接矩阵或邻接表.入度数组 2. 取入度为0的数据(即不依赖其他数据的数据),根据邻接矩阵/邻接表依次减小依赖其的数据的入度 3. 判断减小后是否有新的入度为0的数据,继续进