-
Notifications
You must be signed in to change notification settings - Fork 24
/
lib.rs
52 lines (49 loc) · 1.07 KB
/
lib.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* @lc app=leetcode.cn id=807 lang=rust
*
* [807] 保持城市天际线
*/
struct Solution {}
// @lc code=start
impl Solution {
pub fn max_increase_keeping_skyline(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
if n == 0 {
return 0;
};
let mut res = 0;
let row_max = grid
.iter()
.map(|val| *val.iter().max().unwrap())
.collect::<Vec<i32>>();
let mut col_max: Vec<i32> = vec![-1; n];
grid.iter().enumerate().for_each(|(i, row)| {
row.iter().enumerate().for_each(|(j, val)| {
let max = val.max(&col_max[j]);
col_max[j] = *max;
})
});
grid.iter().enumerate().for_each(|(i, row)| {
row
.iter()
.enumerate()
.for_each(|(j, _)| res += row_max[i].min(col_max[j]) - grid[i][j])
});
return res;
}
}
// @lc code=end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tests() {
let grid = vec![
vec![3, 0, 8, 4],
vec![2, 4, 5, 7],
vec![9, 2, 6, 3],
vec![0, 3, 1, 0],
];
Solution::max_increase_keeping_skyline(grid);
}
}