Max Increase to Keep City Skyline LeetCode solution in C++

1
class Solution {
2
public:
3
int maxIncreaseKeepingSkyline(vector<vector<int>> &grid) {
4
int n = grid.size();
5
// get max for every Column
6
vector<int> maxfromcol(n, INT_MIN);
7
for (int j = 0; j < n; j++) {
8
for (int i = 0; i < n; i++) {
9
maxfromcol[j] = max(maxfromcol[j], grid[i][j]);
10
}
11
}
12
int cost = 0;
13
for (int i = 0; i < n; i++) {
14
// find maximum in ithrow
15
int mx = INT_MIN;
16
for (int j = 0; j < n; j++) {
17
mx = max(mx, grid[i][j]);
18
}
19
cout << mx << endl;
20
// update every element in ith row with min of max of ith row and max of
21
// jth col
22
for (int j = 0; j < n; j++) {
23
int temp = grid[i][j];
24
grid[i][j] = min(mx, maxfromcol[j]);
25
cost += grid[i][j] - temp;
26
}
27
}
28
return cost;
29
}
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0