Skip to content

Commit efc5c63

Browse files
Create max_increase_to_keep_city_skyline.cpp
1 parent 7748ecb commit efc5c63

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
//TC: O(n*2) SC: O(n) => don't think its possible in anything less than n * 2
4+
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
5+
6+
int n = grid.size();
7+
vector<int> maxIncreaseInRow(n, 0);
8+
vector<int> maxIncreaseInCol(n, 0); // n * n
9+
int maxSumIncrease = 0;
10+
11+
12+
//for any skyline problem we consider the max at any index...
13+
//so for any index(i, j) in grid => we can grow the current building to the height of min from maxInCol or maxInRow.
14+
15+
//get max for every row and every column
16+
for(int i = 0; i < n; ++i) {
17+
for(int j = 0; j < n; ++j) {
18+
maxIncreaseInRow[i] = max(maxIncreaseInRow[i], grid[i][j]);
19+
maxIncreaseInCol[j] = max(maxIncreaseInCol[j], grid[i][j]);
20+
}
21+
}
22+
23+
//simple math -
24+
// take min of both maxRow and maxCol at i and j indexes.
25+
// min because we don't want to change the skyline.
26+
// example: maxIncreaseInRow[i] > grid[i][j] => this operation will change the skyline.
27+
for(int i = 0; i < n; ++i) {
28+
for(int j = 0; j < n; ++j) {
29+
maxSumIncrease += min(maxIncreaseInRow[i], maxIncreaseInCol[j]) - grid[i][j];
30+
}
31+
}
32+
33+
return maxSumIncrease;
34+
35+
}
36+
};

0 commit comments

Comments
 (0)