[leetcode]807. Max Increase to Keep City Skyline

[leetcode]807. Max Increase to Keep City Skyline


Analysis

周二快乐~—— [ummm~]

In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be different for different buildings). Height 0 is considered to be a building as well.
At the end, the “skyline” when viewed from all four directions of the grid, i.e. top, bottom, left, and right, must be the same as the skyline of the original grid. A city’s skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. See the following example.
What is the maximum total sum that the height of the buildings can be increased?
比较简单,就跟之前小学做的奥数题一样,保证从前面看和从侧面看的横截面不变~

Implement

class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        int row = grid.size();
        int col = grid[0].size();
        vector<int> r;
        r.resize(row);
        vector<int> c;
        c.resize(col);
        for(int i=0; i<row; i++){
            int tmp = 0;
            for(int j=0; j<col; j++){
                tmp = max(tmp, grid[i][j]);
            }
            r[i] = tmp;
        }
        for(int i=0; i<col; i++){
            int tmp = 0;
            for(int j=0; j<row; j++){
                tmp = max(tmp, grid[j][i]);
            }
            c[i] = tmp;
        }
        int t;
        int res = 0;
        for(int i=0; i<row; i++){
            for(int j=0; j<col; j++){
                t = min(r[i], c[j]);
                res += (t-grid[i][j]);
            }
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/81320163