Max Increase to Keep City Skyline

本文探讨了在保持二维网格四周相同天际线的前提下,如何最大化增加建筑物高度的总和。通过找出每行每列的最大值,计算可以增加的高度,最终达到在不改变天际线的情况下,使建筑物高度总和最大化。

In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be different for different buildings). Height 0 is considered to be a building as well.

At the end, the “skyline” when viewed from all four directions of the grid, i.e. top, bottom, left, and right, must be the same as the skyline of the original grid. A city’s skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. See the following example.

What is the maximum total sum that the height of the buildings can be increased?

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] ]

Notes:

1 < grid.length = grid[0].length <= 50.
All heights grid[i][j] are in the range [0, 100].
All buildings in grid[i][j] occupy the entire grid cell: that is, they are a 1 x 1 x grid[i][j] rectangular prism.

问题剖析

在一个二维素组中,首先我们需要从上到下获取每行里边的最大值,形成一个列表;然后从左到右获取每列中的最大值,形成另外一个列表。 这里需要解决一个会产生冲突的问题,如下所示:

[ [3, 0, 8, 4], 
  [2, 4, 5, 7],
  [9, 2, 6, 3],
  [0, 3, 1, 0] ]

上面这个二维数组,第二列的元素是[0,4,2,3], 第二列的最大元素是4,那么按理说其他不足4的都应该补齐成4.但是还有另外一个限定条件,那就是如果第二列的元素所在行的最大元素必须大于4才可以,但是最后一行的最大元素是3,就不满足条件,此时这列的这个元素只能填充到MIN(当前行的最大值,当前列的最大值),选择两个值中较小的一个座位补充后的目标值。

代码实现如下:

func maxIncreaseKeepingSkyline(grid [][]int) int {
    
    rowsLen := len(grid)
    colsLen := len(grid[0])
    
    maxRows := make([]int, rowsLen)
    maxCols := make([]int, colsLen)
    
    for i := 0; i < rowsLen; i++ {
        for j := 0; j < colsLen; j++ {
            
            gridVal := grid[i][j]
            
            if gridVal > maxRows[i] {
                maxRows[i] = gridVal
            }
            
            if gridVal > maxCols[j] {
                maxCols[j] = gridVal
            }
        }
    }
    
    increaseNum := 0
    for i := 0; i < rowsLen; i++ {
        for j := 0; j < colsLen; j++ {
            maxHeight := maxCols[j]
            if maxRows[i] < maxHeight {
                maxHeight = maxRows[i]
            }
            increaseNum += maxHeight - grid[i][j]
        }
    }
    
    return increaseNum
}
下载前可以先看下教程 https://pan.quark.cn/s/a4b39357ea24 在网页构建过程中,表单(Form)扮演着用户与网站之间沟通的关键角色,其主要功能在于汇集用户的各类输入信息。 JavaScript作为网页开发的核心技术,提供了多样化的API和函数来操作表单组件,诸如input和select等元素。 本专题将详细研究如何借助原生JavaScript对form表单进行视觉优化,并对input输入框与select下拉框进行功能增强。 一、表单基础1. 表单组件:在HTML语言中,<form>标签用于构建一个表单,该标签内部可以容纳多种表单组件,包括<input>(输入框)、<select>(下拉框)、<textarea>(多行文本输入区域)等。 2. 表单参数:诸如action(表单提交的地址)、method(表单提交的协议,为GET或POST)等属性,它们决定了表单的行为特性。 3. 表单行为:诸如onsubmit(表单提交时触发的动作)、onchange(表单元素值变更时触发的动作)等事件,能够通过JavaScript进行响应式处理。 二、input元素视觉优化1. CSS定制:通过设定input元素的CSS属性,例如border(边框)、background-color(背景色)、padding(内边距)、font-size(字体大小)等,能够调整其视觉表现。 2. placeholder特性:提供预填的提示文字,以帮助用户明确输入框的预期用途。 3. 图标集成:借助:before和:after伪元素或者额外的HTML组件结合CSS定位技术,可以在输入框中嵌入图标,从而增强视觉吸引力。 三、select下拉框视觉优化1. 复选功能:通过设置multiple属性...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值