444. Sequence Reconstruction

class Solution {
    public boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) {
        if(org.length == 0 || org.length == 1){
            return true;
        }
        // unique path , every time there is only one node which as indegree 0
        // build a indegree map, key is the node, value is its degree
        // for example, in the first case 
        // the map looks like  key: 1 2 3 
        // .                   val :0 1 1 
        // build another map, key is the node, value is the node is going to 
        // map : key : 1 
        //       val : [2,3]
        
        // no need to use hashmap for indegree, can use int[] 
        // for another map, use hashmap,because the value is the a list of int , 
        // actually can use 2d array for this , 
        
        //initalize data strucurets 
        int[] indegree = new int[org.length + 1]; // ignore 0
        
        //int[][] adjList = new int[org.length + 1][org.length + 1];
        // list index is "from" node , value is  a list of "to" node 
        
        HashMap<Integer,Set<Integer>> adjList = new HashMap<>();
        for(int i = 0; i < adjList.size(); i++){
            adjList.put(i, new HashSet<>());
        }
        // fill in with infor from seqs 
        for(int i = 0; i < seqs.size(); i++){
            for(int j = 0; j < seqs.get(i).size() - 1; j++){
                // [[5,2,6,3],[4,1,5,2]]
                int from = seqs.get(i).get(j);
                int to = seqs.get(i).get(j + 1);
                Set<Integer> set = adjList.get(from);
               
                if(set != null && !set.contains(to)){
                    set.add(to);
                    // indegree 
                    indegree[to]++;
                }
            
            }
        }
        
        Queue<Integer> queue = new LinkedList<>();
        
        // find the node that has indegree = 0 now 
        for(int i = 1; i < indegree.length; i++){
            if(indegree[i] == 0){
                queue.offer(i);
            }
        }
        // 4 has indegree 0 , cuz there is no node goes to 4 
        // adjList : 0 1 2 3 4 5 6   index   from node
        //           e 5 6 e 1 2 3   hashset   to node 
        
        // indegre : 0 1 2 3 4 5 6    index and the node value  
        //           0 1 1 1 0 1 1    the node's indegree 
        
        while(!queue.isEmpty()){
            int size = queue.size();
            if(size > 1) return false;
            int cur = queue.poll();
            for(int nei : adjList.get(cur)){
                indegree[nei] = indegree[nei] - 1;
                if(indegree[nei] == 0){
                    queue.offer(nei);
                }
            }
        }
        return true;
    }
}



// [1,2,3]
// [[1,2],[1,3],[2,3]]
// Output:
// false
// Expected:
// true
    

// This question can be simplified to three conditions:

// TopSort order exists
// Whether the TopSort order is the only one (Uniqueness of Topological sort, Hamilton path, see https://en.wikipedia.org/wiki/Topological_sorting#Uniqueness).如果不是,那么说明有些pair只有偏序关系,没有全序关系,这样不能完全确定元素之间的顺序
// the only top sort order constructed should be equal to the org.
// index == org.length (check condition 3) && index == map.size() (check all the vertex in the graph has been visited, so the top sort order exists, check condition 1)

// How to check only one order? queue.size() should always be one, then only one element at a time has indegree to be 0, so you only have one choice (check condition 2)

猜你喜欢

转载自www.cnblogs.com/tobeabetterpig/p/9563545.html
444