城市天际线--p807

package Array;

public class p807 {
    /**
     * Example:
     Input: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
     Output: 35
     Explanation:
     The grid is:
     [ [3, 0, 8, 4],
     [2, 4, 5, 7],
     [9, 2, 6, 3],
     [0, 3, 1, 0] ]

     The skyline viewed from top or bottom is: [9, 4, 8, 7]
     The skyline viewed from left or right is: [8, 7, 9, 3]

     The grid after increasing the height of buildings without affecting skylines is:

     gridNew = [ [8, 4, 8, 7],
     [7, 4, 7, 7],
     [9, 4, 8, 7],
     [3, 3, 3, 3] ]
     */
    public static  void main(String argv[]){
        int grid[][]={{3,0,8,4},{2,4,5,7},{9,2,6,3},{0,3,1,0}};
        p807 temp=new p807();
        System.out.println(temp.maxIncreaseKeepingSkyline(grid));
    }

    public  int maxIncreaseKeepingSkyline(int[][] grid) {
        int i,j,sum=0;
        int max1[]=new int[grid.length];
        int max2[]=new int[grid[0].length];
        for(i=0;i<grid[0].length;i++){
            for(j=0;j<grid[0].length;j++){
                if(grid[i][j]>max1[i])max1[i]=grid[i][j];
                if(grid[j][i]>max2[i])max2[i]=grid[j][i];
            }
        }
        for(i=0;i<grid.length;i++){
            for(j=0;j<grid[0].length;j++){
                if(max1[i]<max2[j])sum+=max1[i]-grid[i][j];
                else{
                    sum+=max2[j]-grid[i][j];
                }
            }
        }
        return sum;
    }


}

 

转载于:https://www.cnblogs.com/ming-szu/p/8995427.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值