-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths0444_sequence_reconstruction.rs
101 lines (85 loc) · 2.56 KB
/
s0444_sequence_reconstruction.rs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#![allow(unused)]
pub struct Solution {}
use std::collections::HashMap;
impl Solution {
pub fn sequence_reconstruction(org: Vec<i32>, seqs: Vec<Vec<i32>>) -> bool {
let (mut graph, mut indegrees) = (HashMap::new(), HashMap::new());
for seq in seqs.iter() {
for num in seq.iter() {
graph.insert(*num, vec![]);
indegrees.insert(*num, 0);
}
}
for seq in seqs.iter() {
for i in 0..seq.len() - 1 {
let mut neighbors = graph.get(&seq[i]).unwrap().to_vec();
neighbors.push(seq[i + 1]);
graph.insert(seq[i], neighbors);
indegrees.insert(seq[i + 1], indegrees.get(&seq[i + 1]).unwrap() + 1);
}
}
let mut queue = vec![];
for (node, count) in indegrees.iter() {
if *count == 0 {
queue.push(*node);
}
}
let mut res = vec![];
while queue.len() > 0 {
if queue.len() > 1 {
return false;
}
let node = queue.remove(0);
res.push(node);
let neighbors = graph.get(&node).unwrap();
for neighbor in neighbors.iter() {
let indegree = *indegrees.get(neighbor).unwrap() - 1;
indegrees.insert(*neighbor, indegree);
if indegree == 0 {
queue.push(*neighbor);
}
}
}
return res.len() == indegrees.len() && Self::vec_compare(&res, &org);
}
fn vec_compare(a: &Vec<i32>, b: &Vec<i32>) -> bool {
if a.len() != b.len() {
return false;
}
for i in 0..a.len() {
if a[i] != b[i] {
return false;
}
}
return true;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_444() {
assert_eq!(
Solution::sequence_reconstruction(vec![1, 2, 3], vec![vec![1, 2], vec![1, 3]]),
false
);
assert_eq!(
Solution::sequence_reconstruction(vec![1, 2, 3], vec![vec![1, 2]]),
false
);
assert_eq!(
Solution::sequence_reconstruction(
vec![1, 2, 3],
vec![vec![1, 2], vec![1, 3], vec![2, 3]]
),
true
);
assert_eq!(
Solution::sequence_reconstruction(
vec![4, 1, 5, 2, 6, 3],
vec![vec![5, 2, 6, 3], vec![4, 1, 5, 2]]
),
true
);
}
}