自学内容网 自学内容网

Java LeetCode 练习

        807. 保持城市天际线

package JavaExercise20241004;

import java.util.Arrays;

public class JavaExercise {
    public static void main(String[] args) {
        int[][] array = {{3,0,8,4},{2,4,5,7},{9,2,6,3},{0,3,1,0}};

        Solution solution = new Solution();
        System.out.println(solution.maxIncreaseKeepingSkyline(array));
    }
}

class Solution {
    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int n = grid.length;
        int[] rowMax = new int[n];
        int[] colMax = new int[n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                rowMax[i] = Math.max(rowMax[i], grid[i][j]);
                colMax[j] = Math.max(colMax[j], grid[i][j]);
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
            }
        }
        return ans;
    }
}



原文地址:https://blog.csdn.net/Aishangyuwen/article/details/142708258

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!