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;
        
    }
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • JIT, "Just In Time"的缩写,但是它到底是什么意思呢? Whenever a program, w...
    ybin阅读 348评论 0 1
  • p2p理财公司排名2016_华融道理财 p2p理财公司排名2016_华融道理财 p2p理财公司排名2016_华融道理财
    项套粱52499阅读 229评论 0 0
  • 先说说我的故事吧。 我在南方的一个县城长大,家中独子,父亲原来在家这边的一个国营工厂上班。90年代末的时候,父亲工...
    郑毓阅读 387评论 3 1
  • 明天即将迎来清明节小长假第一天,今天蒾鹿大叔想和大家聊一个稍微沉重一点的话题——死亡。 说到“死亡”,你会联想到什...
    蒾鹿大叔阅读 792评论 0 1