I like to read this solution as a small machine: keep the useful information, throw away the noise. For 807. Max Increase to Keep City Skyline, the solution in this repository is mainly a two pointers solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are maxIncreaseKeepingSkyline.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be different for different buildings). Height 0 is considered to be a building as well.
03
04At the end, the "skyline" when viewed from all four directions of the grid, i.e. top, bottom, left, and right, must be the same as the skyline of the original grid. A city's skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. See the following example.
05
06What is the maximum total sum that the height of the buildings can be increased?
07
08Example:
09Input: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
10Output: 35
11Explanation:
12The grid is:
13[ [3, 0, 8, 4],
14 [2, 4, 5, 7],
15 [9, 2, 6, 3],
16 [0, 3, 1, 0] ]
17
18The skyline viewed from top or bottom is: [9, 4, 8, 7]
19The skyline viewed from left or right is: [8, 7, 9, 3]
20
21The grid after increasing the height of buildings without affecting skylines is:
22
23gridNew = [ [8, 4, 8, 7],
24 [7, 4, 7, 7],
25 [9, 4, 8, 7],
26 [3, 3, 3, 3] ]
27
28Notes:
29
301 < grid.length = grid[0].length <= 50.
31All heights grid[i][j] are in the range [0, 100].
32All buildings in grid[i][j] occupy the entire grid cell: that is, they are a 1 x 1 x grid[i][j] rectangular prism.
33**/
34
35/**
36Your runtime beats 44.48 % of cpp submissions.
37**/
38
39/**
40class Solution {
41public:
42 int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
43 vector<int> tb = vector<int>((int)grid[0].size());
44 vector<int> lr = vector<int>((int)grid.size());
45 vector<vector<int> > increased_grid((int)grid.size(), vector<int>((int)grid[0].size()));
46 int answer=0;
47
48 //first find out the skyline
49 for(int j = 0; j < grid[0].size(); j++){
50 int max = 0;
51 for(int i = 0; i < grid.size(); i++){
52 max = grid[i][j]>max?grid[i][j]:max;
53 }
54 tb[j] = max;
55 }
56
57 for(int i = 0; i < grid.size(); i++){
58 int max = 0;
59 for(int j = 0; j < grid[0].size(); j++){
60 max = grid[i][j]>max?grid[i][j]:max;
61 }
62 lr[i] = max;
63 }
64
65 //build the increased skyline
66 for(int i = 0; i < increased_grid.size(); i++){
67 for(int j = 0; j < increased_grid[0].size(); j++){
68 increased_grid[i][j] = tb[i]<lr[j]?tb[i]:lr[j];
69 answer+=(increased_grid[i][j]-grid[i][j]);
70 }
71 }
72
73 return answer;
74 }
75};
76**/
77
78/**
79Intuition and Algorithm
80
81The skyline looking from the top is col_maxes = [max(column_0), max(column_1), ...]. Similarly, the skyline from the left is row_maxes [max(row_0), max(row_1), ...]
82
83In particular, each building grid[r][c] could become height min(max(row_r), max(col_c)), and this is the largest such height. If it were larger, say grid[r][c] > max(row_r), then the part of the skyline row_maxes = [..., max(row_r), ...] would change.
84
85These increases are also independent (none of them change the skyline), so we can perform them independently.
86
87Complexity Analysis
88
89Time Complexity: O(N^2)O(N
902
91 ), where NN is the number of rows (and columns) of the grid. We iterate through every cell of the grid.
92
93Space Complexity: O(N)O(N), the space used by row_maxes and col_maxes.
94**/
95
96//optimized
97class Solution {
98public:
99 int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
100 vector<int> tb = vector<int>((int)grid[0].size(), 0);
101 vector<int> lr = vector<int>((int)grid.size(), 0);
102 int answer=0;
103
104 //first find out the skyline
105 for(int i = 0; i < grid.size(); i++){
106 for(int j = 0; j < grid[0].size(); j++){
107 lr[i] = max(grid[i][j], lr[i]); //compare through j, left-right
108 tb[j] = max(grid[i][j], tb[j]); //compare through i, top-bottom
109 }
110 }
111
112 //build the increased skyline
113 for(int i = 0; i < grid.size(); i++){
114 for(int j = 0; j < grid[0].size(); j++){
115 answer+=(min(tb[i], lr[j])-grid[i][j]);
116 }
117 }
118
119 return answer;
120 }
121};
Cost