807. Max Increase to Keep City Skyline Medium

@problem@discussion
#Array#Greedy#Matrix



1/**
2 * [807] Max Increase to Keep City Skyline
3 *
4 * There is a city composed of n x n blocks, where each block contains a single building shaped like a vertical square prism. You are given a 0-indexed n x n integer matrix grid where grid[r][c] represents the height of the building located in the block at row r and column c.
5 * A city's skyline is the the outer contour formed by all the building when viewing the side of the city from a distance. The skyline from each cardinal direction north, east, south, and west may be different.
6 * We are allowed to increase the height of any number of buildings by any amount (the amount can be different per building). The height of a 0-height building can also be increased. However, increasing the height of a building should not affect the city's skyline from any cardinal direction.
7 * Return the maximum total sum that the height of the buildings can be increased by without changing the city's skyline from any cardinal direction.
8 *  
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/21/807-ex1.png" style="width: 700px; height: 603px;" />
11 * Input: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
12 * Output: 35
13 * Explanation: The building heights are shown in the center of the above image.
14 * The skylines when viewed from each cardinal direction are drawn in red.
15 * The grid after increasing the height of buildings without affecting skylines is:
16 * gridNew = [ [8, 4, 8, 7],
17 *             [7, 4, 7, 7],
18 *             [9, 4, 8, 7],
19 *             [3, 3, 3, 3] ]
20 * 
21 * Example 2:
22 * 
23 * Input: grid = [[0,0,0],[0,0,0],[0,0,0]]
24 * Output: 0
25 * Explanation: Increasing the height of any building will result in the skyline changing.
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	n == grid.length
31 * 	n == grid[r].length
32 * 	2 <= n <= 50
33 * 	0 <= grid[r][c] <= 100
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/max-increase-to-keep-city-skyline/
39// discuss: https://leetcode.com/problems/max-increase-to-keep-city-skyline/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn max_increase_keeping_skyline(grid: Vec<Vec<i32>>) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_807() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.