row_vector and col_vector的建立 (Leetcode 807, Leetcode 531)
這兩道題都需要建立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;
}
};
相關文章
- 【LeetCode】如何學習LeetCode?LeetCode
- 我的 LeetCodeLeetCode
- [LeetCode刷題筆記] 關於LeetCode的前言LeetCode筆記
- LeetCodeLeetCode
- LeetCode - 1389 - 按既定順序建立目標陣列LeetCode陣列
- [LeetCode] Construct Quad Tree 建立四叉樹LeetCodeStruct
- LeetCode 164 最大間距 HERODING的LeetCode之路LeetCode
- LeetCode 143 重排連結串列 HERODING的LeetCode之路LeetCode
- LeetCode in actionLeetCode
- Leetcode AnagramsLeetCode
- Leetcode SortListLeetCode
- Leetcode ReorderListLeetCode
- leetcode SubsetsLeetCode
- Leetcode PermutationsLeetCode
- [leetcode]有效的括號LeetCode
- leetcode:有效的數獨LeetCode
- LeetCode 402 移掉K位數字 HERODING的LeetCode之路LeetCode
- Leetcode學習LeetCode
- LeetCode 53 JSLeetCodeJS
- LeetCode 50 JSLeetCodeJS
- LeetCode 克隆圖LeetCode
- Leetcode Perfect SquaresLeetCode
- Leetcode Sort ColorsLeetCode
- Leetcode Sort ArrayLeetCode
- 【LeetCode】Jewels and StonesLeetCode
- 嘗試 LeetcodeLeetCode
- LeetCode 消除遊戲LeetCode遊戲
- Leetcode03LeetCode
- Leetcode LRU CacheLeetCode
- Leetcode Path SumLeetCode
- LeetCode Min StackLeetCode
- Leetcode Word SearchLeetCode
- Leetcode Number of islandsLeetCode
- Leetcode-394LeetCode
- Leetcode 921 JavascriptLeetCodeJavaScript
- golang刷leetcodeGolangLeetCode
- LeetCode:快速排序LeetCode排序
- [LeetCode] Group AnagramLeetCode