diff options
Diffstat (limited to 'most_stones_remove.rs')
-rw-r--r-- | most_stones_remove.rs | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/most_stones_remove.rs b/most_stones_remove.rs index 8dc890d..d32137f 100644 --- a/most_stones_remove.rs +++ b/most_stones_remove.rs @@ -1,3 +1,14 @@ +// 947. Most Stones Removed with Same Row or Column + +/* + * on a 2d plane, we place 'n' stones at some integer coordinate points. each + * coordinate point may have at most one stone. a stone can be removed if it + * shares either the same row or the same column as another stone that has not + * been removed. given an array 'stones' of length 'n' where 'stones[i] = [x_i, + * y_i]' represents the location of the i'th stone, return the largest possible + * number of stones that can be removed + */ + use std::collections::HashMap; struct Solution; @@ -9,16 +20,24 @@ impl Solution { let mut cols: HashMap<i32, usize> = HashMap::new(); for (i, v) in stones.iter().enumerate() { match rows.get(&v[0]) { - Some(&j) => if Self::union_stone(i, j, &mut parent) { - ans += 1; + Some(&j) => { + if Self::union_stone(i, j, &mut parent) { + ans += 1; + } + } + None => { + rows.insert(v[0], i); } - None => { rows.insert(v[0], i); } } match cols.get(&v[1]) { - Some(&j) => if Self::union_stone(i, j, &mut parent) { - ans += 1; + Some(&j) => { + if Self::union_stone(i, j, &mut parent) { + ans += 1; + } + } + None => { + cols.insert(v[1], i); } - None => { cols.insert(v[1], i); } } } ans @@ -39,11 +58,12 @@ impl Solution { fn main() { let stones = vec![ - vec![0,0], - vec![0,1], - vec![1,0], - vec![1,2], - vec![2,1], - vec![2,2]]; + vec![0, 0], + vec![0, 1], + vec![1, 0], + vec![1, 2], + vec![2, 1], + vec![2, 2], + ]; print!("{}", Solution::remove_stones(stones)); } |