-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path807.cpp
38 lines (31 loc) · 787 Bytes
/
807.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
int rows = grid.size();
int cols = grid[0].size();
vector<int> row_max(rows, 0);
vector<int> col_max(cols, 0);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] > row_max[i]) {
row_max[i] = grid[i][j];
}
if (grid[i][j] > col_max[j]) {
col_max[j] = grid[i][j];
}
}
}
int diff = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
int lim = min(row_max[i], col_max[j]);
if (grid[i][j] < lim) {
diff += lim - grid[i][j];
}
}
return diff;
}
};