这两道题都需要建立row_vector and col_vector, 来统计每一行和每一列的信息。
Leetcode 807:
https://leetcode.com/problems/max-increase-to-keep-city-skyline/description/
class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
if(grid.empty() || grid[0].empty()){
return 0;
}
int n = grid.size();
vector<int> row_record(n, 0), col_record(n, 0);
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
row_record[i] = max(row_record[i], grid[i][j]);
col_record[j] = max(col_record[j], grid[i][j]);
}
}
int total_sum = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
total_sum += min(row_record[i], col_record[j]) - grid[i][j];
}
}
return total_sum;
}
};
Leetcode 531:
https://leetcode.com/problems/lonely-pixel-i/description/
class Solution {
public:
int findLonelyPixel(vector<vector<char>>& picture) {
if(picture.empty() || picture[0].empty()){
return 0;
}
int row = picture.size(), col = picture[0].size();
vector<int> row_record(row, 0);
vector<int> col_record(col, 0);
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
if(picture[i][j] == 'B'){
row_record[i]++;
col_record[j]++;
}
}
}
int cnt = 0;
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
if(picture[i][j] == 'B'){
if(row_record[i] == 1 && col_record[j] == 1){
cnt++;
}
}
}
}
return cnt;
}
};
网友评论