-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0939_minimum_area_rectangle.rs
More file actions
58 lines (53 loc) · 1.3 KB
/
s0939_minimum_area_rectangle.rs
File metadata and controls
58 lines (53 loc) · 1.3 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
#![allow(unused)]
pub struct Solution {}
// google interview
impl Solution {
pub fn min_area_rect(points: Vec<Vec<i32>>) -> i32 {
use std::cmp::min;
use std::collections::HashSet;
let mut visited = HashSet::<(i32, i32)>::new();
let mut ans = std::i32::MAX;
for p in &points {
for &(x2, y2) in &visited {
if visited.contains(&(p[0], y2)) && visited.contains(&(x2, p[1])) {
let area = ((p[0] - x2) * (p[1] - y2)).abs();
ans = min(area, ans);
}
}
visited.insert((p[0], p[1]));
}
if ans == std::i32::MAX {
0
} else {
ans
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_939() {
assert_eq!(
Solution::min_area_rect(vec![
vec![1, 1],
vec![1, 3],
vec![3, 1],
vec![3, 3],
vec![2, 2]
]),
4
);
assert_eq!(
Solution::min_area_rect(vec![
vec![1, 1],
vec![1, 3],
vec![3, 1],
vec![3, 3],
vec![4, 1],
vec![4, 3]
]),
2
);
}
}