1. 程式人生 > >Leetcode 807. Max Increase to Keep City Skyline

Leetcode 807. Max Increase to Keep City Skyline

class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        if (grid.empty()) return 0;
        int m = grid.size(), n = grid[0].size();
    
        vector<int> skyline_td(n, INT_MIN);
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < m; i++) {
                skyline_td[j] = std::max(skyline_td[j], grid[i][j]);
            }
        }
    
        vector<int> skyline_lr(m, INT_MIN);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                skyline_lr[i] = std::max(skyline_lr[i], grid[i][j]);
            }
        }
    
        int sum = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                sum += std::min(skyline_td[j], skyline_lr[i]) - grid[i][j];
            }
        }
    
        return sum;
    }
};