Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2219 from MartinLwx/rust/0554-brick-wall
Browse files Browse the repository at this point in the history
Create: 0554-brick-wall.rs
  • Loading branch information
tahsintunan authored Feb 10, 2023
2 parents b382127 + 74a84df commit c9b9f1b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions rust/0554-brick-wall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::collections::HashMap;
impl Solution {
pub fn least_bricks(wall: Vec<Vec<i32>>) -> i32 {
wall.len() as i32
- wall
.into_iter()
.map(|row| {
let mut prefix: Vec<_> = row
.into_iter()
.scan(0, |sum, x| {
*sum += x;
Some(*sum)
})
.collect();
prefix.pop();
prefix
})
.flatten()
.fold(HashMap::from([(0, 0)]), |mut acc, x| {
acc.entry(x).and_modify(|cnt| *cnt += 1).or_insert(1);
acc
})
.into_iter()
.map(|(_, v)| v)
.max()
.unwrap()
}
}

0 comments on commit c9b9f1b

Please sign in to comment.