https://leetcode.com/problems/max-increase-to-keep-city-skyline/

Source


    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        const int n = grid.size();
        vi viRowMax(n, 0);
        vi viColMax(n, 0);
        FOR (r, n) {
            FOR (c, n) {
                const int val = grid[r][c];
                if (val > viRowMax[r]) {
                    viRowMax[r] = val;
                }
                if (val > viColMax[c]) {
                    viColMax[c] = val;
                }
            }
        }

        int sum = 0;
        FOR (r, n) {
            FOR (c, n) {
                const int limit = min(viRowMax[r], viColMax[c]);
                if (limit > grid[r][c]) {
                    sum += limit - grid[r][c];
                }
            }
        }
        return sum;
    }

GitHub

MaxIncreaseKeepingSkyline