Skip to content

Commit 62908f2

Browse files
committed
src/bin/find-the-town-judge.rs
1 parent c73784e commit 62908f2

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/bin/find-the-town-judge.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![allow(dead_code, unused, unused_variables, non_snake_case)]
2+
3+
use std::future::ready;
4+
5+
fn main() {}
6+
7+
struct Solution;
8+
9+
impl Solution {
10+
pub fn find_judge(n: i32, trust: Vec<Vec<i32>>) -> i32 {
11+
let mut map = std::collections::HashMap::new();
12+
let mut m = std::collections::HashSet::new();
13+
14+
for v in trust {
15+
map.entry(v[1]).and_modify(|x| *x += 1).or_insert(1);
16+
m.insert(v[0]);
17+
}
18+
19+
let mut result = None;
20+
for i in 1..=n {
21+
let v = map.get(&i).unwrap_or(&0);
22+
if *v == n - 1 && !m.contains(&i) {
23+
if result.is_none() {
24+
result = Some(i);
25+
} else {
26+
return -1;
27+
}
28+
}
29+
}
30+
31+
result.unwrap_or(-1)
32+
}
33+
}

0 commit comments

Comments
 (0)