Leetcode 807. Max Increase to Keep City Skyline
阿新 • • 發佈:2018-12-14
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; } };