The sword refers to offer115: reconstruction sequence

Question:
Please determine whether the original sequence org can be uniquely reconstructed from the sequence set seqs.
The sequence org is a permutation of integers from 1 to n, where 1 ≤ n ≤ 104. Reconstruction refers to constructing the shortest common supersequence in the sequence set seqs, that is, any sequence in seqs is a subsequence of the shortest sequence.
Example 1:
Input: org = [1,2,3], seqs = [[1,2],[1,3]]
Output: false
Explanation: [1,2,3] is not the only sequence that can be reconstructed , because [1,3,2] is also a valid sequence.
Example 2:
Input: org = [1,2,3], seqs = [[1,2]]
Output: false
Explanation: The sequence that can be reconstructed is only [1,2].
Example 3:
Input: org = [1,2,3], seqs = [[1,2],[1,3],[2,3]]
Output: true
Explanation: sequence [1,2], [1 ,3] and [2,3] can be uniquely reconstructed as 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
Analysis:
Breadth-first search plus topological sorting. Breadth-first uses queues. If there are two elements in the queue, it proves that org is not unique, so the queue requires only one element at most, and the directed graph uses the HashMap type graph in the form of an adjacency list. Save, and count the in-degree of each node and save it to another HashMap type inDefrees.

Code:

package com.kuang;

import java.util.*;
import java.util.stream.Stream;

public class SequenceReconstruction {
    
    
    public boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) {
    
    
        Map<Integer, Set<Integer>> graph = new HashMap<>();
        Map<Integer, Integer> inDegrees = new HashMap<>();
        for (List<Integer> seq: seqs){
    
    
            for (int num : seq) {
    
    
                if (num < 1 || num > org.length){
    
    
                    return false;
                }
                graph.putIfAbsent(num,new HashSet<>());
                inDegrees.putIfAbsent(num,0);
            }
            for (int i = 0; i < seq.size() - 1; i++) {
    
    
                int num1 = seq.get(i);
                int num2 = seq.get(i+1);
                if (!graph.get(num1).contains(num2)){
    
    
                    graph.get(num1).add(num2);
                    inDegrees.put(num2,inDegrees.get(num2)+1);
                }
            }
        }
        Queue<Integer> queue = new LinkedList<>();
        for (int num : inDegrees.keySet()) {
    
    
            if (inDegrees.get(num) == 0){
    
    
                queue.add(num);
            }
        }
//        用来和org比较是否相等
        List<Integer> built = new LinkedList<>();
//        队列中元素如果数量大于1,就证明一定不是唯一的,返回false就完了
        while (queue.size() ==1){
    
    
            int num = queue.remove();
            built.add(num);
            for (int next : graph.get(num)){
    
    
                inDegrees.put(next,inDegrees.get(next)-1);
                if (inDegrees.get(next) == 0){
    
    
                    queue.add(next);
                }
            }
        }
        int[] result = built.stream().mapToInt(a -> a).toArray();
        return Arrays.equals(org,result);
    }
}

insert image description here

Guess you like

Origin blog.csdn.net/Jiaodaqiaobiluo/article/details/124013874