Sequence Reconstruction
public class Solution {
public boolean sequenceReconstruction(int[] org, int[][] seqs) {
Map<Integer, Set<Integer>> map = new HashMap<>();
Map<Integer, Integer> indegree = new HashMap<>();
for(int[] seq: seqs) {
if(seq.length == 1) {
if(!map.containsKey(seq[0])) {
map.put(seq[0], new HashSet<>());
indegree.put(seq[0], 0);
}
} else {
for(int i = 0; i < seq.length - 1; i++) {
if(!map.containsKey(seq[i])) {
map.put(seq[i], new HashSet<>());
indegree.put(seq[i], 0);
}
if(!map.containsKey(seq[i + 1])) {
map.put(seq[i + 1], new HashSet<>());
indegree.put(seq[i + 1], 0);
}
if(map.get(seq[i]).add(seq[i + 1])) {
indegree.put(seq[i + 1], indegree.get(seq[i + 1]) + 1);
}
}
}
}
Queue<Integer> queue = new LinkedList<>();
for(Map.Entry<Integer, Integer> entry: indegree.entrySet()) {
if(entry.getValue() == 0) queue.offer(entry.getKey());
}
int index = 0;
while(!queue.isEmpty()) {
int size = queue.size();
if(size > 1) return false;
int curr = queue.poll();
if(index == org.length || curr != org[index++]) return false;
for(int next: map.get(curr)) {
indegree.put(next, indegree.get(next) - 1);
if(indegree.get(next) == 0) queue.offer(next);
}
}
return index == org.length && index == map.size();
}
}
from collections import defaultdict
class Solution(object):
def sequenceReconstruction(self, org, seqs):
"""
:type org: List[int]
:type seqs: List[List[int]]
:rtype: bool
"""
adjacent = defaultdict(list)
incoming_nodes = defaultdict(int)
nodes = set()
for arr in seqs:
nodes |= set(arr)
for i in xrange(len(arr)):
if i == 0:
incoming_nodes[arr[i]] += 0
if i < len(arr) - 1:
adjacent[arr[i]].append(arr[i + 1])
incoming_nodes[arr[i + 1]] += 1
cur = [k for k in incoming_nodes if incoming_nodes[k] == 0]
res = []
while len(cur) == 1:
cur_node = cur.pop()
res.append(cur_node)
for node in adjacent[cur_node]:
incoming_nodes[node] -= 1
if incoming_nodes[node] == 0:
cur.append(node)
if len(cur) > 1:
return False
return len(res) == len(nodes) and res == org
最后更新于
这有帮助吗?