Skip to content

Latest commit

 

History

History
53 lines (39 loc) · 1.36 KB

_807. Max Increase to Keep City Skyline.md

File metadata and controls

53 lines (39 loc) · 1.36 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 22, 2024

Last updated : June 22, 2024


Related Topics : Array, Greedy, Matrix

Acceptance Rate : 86.23 %


Solutions

Java

class Solution {
    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int[] maxByCol = new int[grid[0].length];
        int[] maxByRow = new int[grid.length];

        for (int i = 0; i < grid.length; i++) {
            maxByRow[i] = grid[i][0];
            maxByCol[i] = grid[0][i];

            for (int j = 1; j < grid[0].length; j++) {
                maxByRow[i] = Integer.max(maxByRow[i], grid[i][j]);
                maxByCol[i] = Integer.max(maxByCol[i], grid[j][i]);
            }
        }

        int output = 0;

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                output += Integer.min(maxByRow[i], maxByCol[j]) - grid[i][j];
            }
        }
        return output;
    }
}