forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2013-detect-squares.rs
More file actions
35 lines (29 loc) · 843 Bytes
/
2013-detect-squares.rs
File metadata and controls
35 lines (29 loc) · 843 Bytes
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
use std::collections::HashMap;
struct DetectSquares {
points: Vec<(i32, i32)>,
counts: HashMap<(i32, i32), i32>
}
impl DetectSquares {
fn new() -> Self {
Self {
points: vec![],
counts: HashMap::new()
}
}
fn add(&mut self, point: Vec<i32>) {
let p = (point[0], point[1]);
self.points.push(p);
*self.counts.entry(p).or_default() += 1;
}
fn count(&self, point: Vec<i32>) -> i32 {
let mut res = 0;
let (px, py) = (point[0], point[1]);
for (x, y) in self.points.iter() {
if (py - y).abs() != (px - x).abs() || *x == px || *y == py {
continue;
}
res += self.counts.get(&(*x, py)).unwrap_or(&0) * self.counts.get(&(px,*y)).unwrap_or(&0);
}
res
}
}