1. 程式人生 > >LeetCode - 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 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