Tuesday, September 8, 2020

LeetCode [444] Sequence Reconstruction

 444. Sequence Reconstruction

Medium

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

 

Constraints:

  • 1 <= n <= 10^4
  • org is a permutation of {1,2,...,n}.
  • 1 <= segs[i].length <= 10^5
  • seqs[i][j] fits in a 32-bit signed integer.

 

UPDATE (2017/1/8):
The seqs parameter had been changed to a list of list of strings (instead of a 2d array of strings). Please reload the code definition to get the latest changes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
    public boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) {
        Map<Integer, Set<Integer>> links = new HashMap<>();
        Map<Integer, Integer> degrees = new HashMap<>();

        for(List<Integer> s : seqs){
            for(int i = 0; i<s.size(); ++i){
                degrees.computeIfAbsent(s.get(i), k -> 0);
                links.computeIfAbsent(s.get(i), k -> new HashSet<>());
                if(i<s.size()-1){
                    degrees.computeIfAbsent(s.get(i+1), k -> 0);
                    if(!links.get(s.get(i)).contains(s.get(i+1))){
                        links.get(s.get(i)).add(s.get(i+1));
                        degrees.compute(s.get(i+1), (k,v) -> v=v+1);
                    }
                }
            }
        }
        if(links.size()!=org.length) return false;

        Queue<Integer> que = new LinkedList<>();
        for(Map.Entry<Integer, Integer> e : degrees.entrySet()){
            if(e.getValue()==0) que.offer(e.getKey());
        }

        int index = 0;
        while(!que.isEmpty()){
            if(que.size()>1) return false;
            int top = que.poll();
            if(index==org.length || top!=org[index++]) return false;
            for(int next : links.get(top)){
                degrees.compute(next, (k,v)->v=v-1);
                if(degrees.get(next)==0) que.offer(next);
            }
        }

        return index == org.length;
    }
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
    public boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) {
        if(seqs == null || seqs.size() == 0) return false;
        int orgLen = org.length;
        int[] idx = new int[orgLen + 1];
        boolean[] pairs = new boolean[orgLen];//true: (org[i]->org[i+1]) exists
        
        for(int i = 0; i < orgLen; i++) idx[org[i]] = i;
        
        for(List<Integer> seq : seqs){
            for(int i = 0; i < seq.size(); i++){
                if(seq.get(i) > orgLen || seq.get(i) <= 0) return false;
                if(i>0){
                    int a = seq.get(i-1), b = seq.get(i);
                    if(idx[a]>=idx[b]) return false;
                    if(idx[a]+1==idx[b]) pairs[idx[a]] = true;
                }
            }
        }
        
        for(int i = 0; i < orgLen - 1; i++){
            if(!pairs[i]) return false;
        }
        
        return true;
    }
}

No comments:

Post a Comment