-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0399_evaluate_division.rs
More file actions
75 lines (70 loc) · 2.4 KB
/
s0399_evaluate_division.rs
File metadata and controls
75 lines (70 loc) · 2.4 KB
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
#![allow(unused)]
pub struct Solution {}
use std::collections::{HashMap, HashSet, VecDeque};
impl Solution {
// Let N be the number of input equations and M be the number of queries.
//Time Complexity: O(M⋅N)
pub fn calc_equation(
equations: Vec<Vec<String>>,
values: Vec<f64>,
queries: Vec<Vec<String>>,
) -> Vec<f64> {
// code here
let mut graph: HashMap<&String, Vec<(f64, &String)>> = HashMap::new();
for i in 0..equations.len() {
(*graph.entry(&equations[i][0]).or_insert(vec![])).push((values[i], &equations[i][1]));
(*graph.entry(&equations[i][1]).or_insert(vec![]))
.push((1.0 / values[i], &equations[i][0]));
}
queries
.into_iter()
.map(|v| {
let mut seen = HashSet::new();
let mut q: VecDeque<(f64, &String)> = VecDeque::new();
if let Some(edges) = graph.get(&v[0]) {
for edge in edges {
q.push_back(edge.clone());
}
}
while let Some((val, s)) = q.pop_front() {
seen.insert(s.to_owned());
if s == &v[1] {
return val;
} else {
for edge in graph.get(&s).unwrap_or(&vec![]) {
if !seen.contains(edge.1) {
q.push_back((val * edge.0, edge.1));
}
}
}
}
-1.0
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_399() {
// code here
assert_eq!(
Solution::calc_equation(
vec![
vec!["a".to_string(), "b".to_string()],
vec!["b".to_string(), "c".to_string()],
],
vec![2.0, 3.0],
vec![
vec!["a".to_string(), "c".to_string()],
vec!["b".to_string(), "a".to_string()],
vec!["a".to_string(), "e".to_string()],
vec!["a".to_string(), "a".to_string()],
vec!["x".to_string(), "x".to_string()]
]
),
vec![6.00000, 0.50000, -1.00000, 1.00000, -1.00000]
);
}
}