LeetCode - Sequence Reconstruction

Posted IncredibleThings

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

用两个哈希表来代替了上面的数组和变量,其中m为数字和其位置之间的映射,pre为当前数字和其前一个位置的数字在org中的位置之间的映射。跟上面的方法的不同点在于,当遍历到某一个数字的时候,我们看当前数字是否在pre中有映射,如果没有的话,我们建立该映射,注意如果是第一个位置的数字的话,其前面数字设为-1。如果该映射存在的话,我们对比前一位数字在org中的位置和当前的映射值的大小,取其中较大值。最后我们遍历一遍org,看每个数字的映射值是否是前一个数字的位置,如果有不是的返回false,全部验证成功返回true,参见代码如下:

// "static void main" must be defined in a public class.
public class Main {
    public static void main(String[] args) {
        int[] org = {4,1,5,2,6,3};
        List<List<Integer>> seqs = new ArrayList<>();
        List<Integer> seq1 = new ArrayList<>();
        seq1.add(5);
        seq1.add(2);
        seq1.add(6);
        seq1.add(3);
        
        List<Integer> seq2 = new ArrayList<>();
        seq2.add(4);
        seq2.add(1);
        seq2.add(5);
        seq2.add(2);
        
        seqs.add(seq1);
        seqs.add(seq2);
        
        System.out.println(new Solution().sequenceReconstruction(org, seqs));
    }
}

class Solution {
    public boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) {
        if(org == null || org.length == 0 || seqs == null || seqs.size() == 0){
            return false;
        }
        Map<Integer, Integer> map = new HashMap<>();
        Map<Integer, Integer> pre = new HashMap<>();
        
        for(int i = 0 ; i < org.length ; i++){
            map.put(org[i], i);
        }
        for(List<Integer> seq : seqs){
            for(int i = 0; i< seq.size(); i++){
                if(!map.containsKey(seq.get(i))){
                    return false;
                }
                if(i > 0 && map.get(seq.get(i-1)) >= map.get(seq.get(i))){
                    return false;
                }
                if(!pre.containsKey(seq.get(i))){
                    if(i == 0){
                        pre.put(seq.get(i), -1);
                    }
                    else{
                        pre.put(seq.get(i), map.get(seq.get(i-1)));
                    }
                }
                else{
                    if(i == 0){
                        pre.put(seq.get(i), Math.max(pre.get(seq.get(i)), -1));
                    }
                    else{
                        pre.put(seq.get(i), Math.max(pre.get(seq.get(i)), map.get(seq.get(i-1))));
                    }
                }
            }
        }
        for(int i = 0; i < org.length; i++){
            if(pre.get(org[i]) != i - 1){
                return false;
            }
        }
        return true;
    }
}

 还可以用 Topological Sorting

以上是关于LeetCode - Sequence Reconstruction的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode(60):Permutation Sequence

LeetCode: Longest Consecutive Sequence [128]

[Lintcode]124. Longest Consecutive Sequence/[Leetcode]128. Longest Consecutive Sequence

一天一道LeetCode#60. Permutation Sequence.

LeetCode Verify Preorder Sequence in Binary Search Tree

LeetCode: 60. Permutation Sequence(Medium)