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] Construct Quad Tree 建立四叉樹LeetCodeStruct
- 【LeetCode】如何學習LeetCode?LeetCode
- LeetCode 164 最大間距 HERODING的LeetCode之路LeetCode
- 我的 LeetCodeLeetCode
- LeetCode 143 重排連結串列 HERODING的LeetCode之路LeetCode
- leetcodeLeetCode
- LeetCode 402 移掉K位數字 HERODING的LeetCode之路LeetCode
- LeetCode - 1389 - 按既定順序建立目標陣列LeetCode陣列
- LeetCode in actionLeetCode
- leetcode 238LeetCode
- LeetCode 188 買賣股票的最佳時機IV HERODING的LeetCode之路LeetCode
- LeetCode 100——相同的樹LeetCode
- leetcode 231 2的冪LeetCode
- [leetcode]有效的括號LeetCode
- leetcode:有效的數獨LeetCode
- LeetCode問題LeetCode
- 【LeetCode】Jewels and StonesLeetCode
- Leetcode 513 javascriptLeetCodeJavaScript
- LeetCode 162 JavascriptLeetCodeJavaScript
- Leetcode 921 JavascriptLeetCodeJavaScript
- [Leetcode] Edit DistanceLeetCode
- LeetCode 克隆圖LeetCode
- LeetCode #378 JavaScriptLeetCodeJavaScript
- LeetCode 747 JavaScriptLeetCodeJavaScript
- Leetcode725LeetCode
- LeetCode 53 JSLeetCodeJS
- LeetCode 50 JSLeetCodeJS
- Leetcode03LeetCode
- LeetCode 799 JavaScriptLeetCodeJavaScript
- leetcode690LeetCode
- [LeetCode] Group AnagramLeetCode
- [LeetCode] Rotate StringLeetCode
- LeetCode 283 jsLeetCodeJS
- Leetcode #463 JavascriptLeetCodeJavaScript
- Leetcode # 495 JavascriptLeetCodeJavaScript
- Leetcode 229 JavaScriptLeetCodeJavaScript
- golang刷leetcodeGolangLeetCode
- Leetcode目錄LeetCode