forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0554-brick-wall.rs
28 lines (28 loc) · 881 Bytes
/
0554-brick-wall.rs
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
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()
}
}