Skip to content

Commit 30af504

Browse files
authored
Create max-increase-to-keep-city-skyline.cpp
1 parent d373308 commit 30af504

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n^2)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
7+
vector<int> row_maxes(grid.size());
8+
vector<int> col_maxes(grid[0].size());
9+
for (int r = 0; r < grid.size(); ++r) {
10+
for (int c = 0; c < grid[r].size(); ++c) {
11+
row_maxes[r] = max(row_maxes[r], grid[r][c]);
12+
col_maxes[c] = max(col_maxes[c], grid[r][c]);
13+
}
14+
}
15+
int result = 0;
16+
for (int r = 0; r < grid.size(); ++r) {
17+
for (int c = 0; c < grid[r].size(); ++c) {
18+
result += min(row_maxes[r], col_maxes[c]) - grid[r][c];
19+
}
20+
}
21+
return result;
22+
}
23+
};

0 commit comments

Comments
 (0)