来源:力扣(LeetCode)

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

题目描述

在二维数组grid中,grid[i][j]代表位于某处的建筑物的高度。 我们被允许增加任何数量(不同建筑物的数量可能不同)的建筑物的高度。 高度 0 也被认为是建筑物。

最后,从新数组的所有四个方向(即顶部,底部,左侧和右侧)观看的“天际线”必须与原始数组的天际线相同。 城市的天际线是从远处观看时,由所有建筑物形成的矩形的外部轮廓。 请看下面的例子。

建筑物高度可以增加的最大总和是多少?

例子:
输入: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
输出: 35
解释:
The grid is:
[ [3, 0, 8, 4],
[2, 4, 5, 7],
[9, 2, 6, 3],
[0, 3, 1, 0] ]

从数组竖直方向(即顶部,底部)看“天际线”是:[9, 4, 8, 7]
从水平水平方向(即左侧,右侧)看“天际线”是:[8, 7, 9, 3]

在不影响天际线的情况下对建筑物进行增高后,新数组如下:

gridNew = [ [8, 4, 8, 7],
[7, 4, 7, 7],
[9, 4, 8, 7],
[3, 3, 3, 3] ]

解题思路

个人认为这道题难度并达不到中等难度

城市建筑物构成了m*n的一个矩阵,不改变城市天际线,就表示每行每列的最大值不能改变,每个建筑物最大高度是行最大值和列最大值中的最小值。那么经过一次遍历找到行最大值和列最大值,再经过一次遍历找到增加后的建筑物高度和,减去增加前的高度和,此题得解。

源码展示

class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
vector<int> viMaxL, viMaxT;
int iSum = 0;
int iM = grid.size();
int iN = grid[0].size();
viMaxL.resize(iM, 0);
viMaxT.resize(iN, 0); for(int i = 0; i< iM; i++)
{
for(int j = 0; j<iN; j++)
{
if(grid[i][j] > viMaxL[i])
{
viMaxL[i] = grid[i][j];
}
if(grid[i][j] > viMaxT[j])
{
viMaxT[j] = grid[i][j];
}
iSum -= grid[i][j];
}
}
for(int i = 0; i< iM; i++)
{
for(int j = 0; j<iN; j++)
{
iSum += min(viMaxL[i], viMaxT[j]);
}
}
return iSum;
}
};

运行结果

LeetCode-807 保持城市天际线的更多相关文章

  1. Java实现 LeetCode 807 保持城市天际线 (暴力)

    807. 保持城市天际线 在二维数组grid中,grid[i][j]代表位于某处的建筑物的高度. 我们被允许增加任何数量(不同建筑物的数量可能不同)的建筑物的高度. 高度 0 也被认为是建筑物. 最后 ...

  2. 每日一题-——LeetCode(807)保持城市天际线

    题目描述: 在二维数组grid中,grid[i][j]代表位于某处的建筑物的高度. 我们被允许增加任何数量(不同建筑物的数量可能不同)的建筑物的高度. 高度 0 也被认为是建筑物.最后,从新数组的所有 ...

  3. LeetCode #807. Max Increase to Keep City Skyline 保持城市天际线

    https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/ 执行用时 : 3 ms, 在Max Increase to Ke ...

  4. [LeetCode] Max Increase to Keep City Skyline 保持城市天际线的最大增高

    In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...

  5. Leetcode 807 Max Increase to Keep City Skyline 不变天际线

    Max Increase to Keep City Skyline In a 2 dimensional array grid, each value grid[i][j] represents th ...

  6. [Swift]LeetCode807. 保持城市天际线 | Max Increase to Keep City Skyline

    In a 2 dimensional array grid, each value grid[i][j]represents the height of a building located ther ...

  7. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  8. Leetcode 807. Max Increase to Keep City Skyline

    class Solution(object): def maxIncreaseKeepingSkyline(self, grid): """ :type grid: Li ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  10. 2019年9月Leetcode每日训练日志

    2019-09-16 #1171 从链表中删去总和值为零的连续节点 #1170 比较字符串最小字母出现频次 #1169 查询无效交易 #226 翻转二叉树 2019-09-15 #1190 反转每对括 ...

随机推荐

  1. pycharm 2021.2.1专业版破解

    1.网址:https://gitee.com/pengzhile/ide-eval-resetter 2.点击下载.下载后直接丢进pycharm中. 3.勾选.重启 .查看

  2. Django框架三板斧本质-jsonResponse对象-form表单上传文件request对象方法-FBV与CBV区别

    目录 一:视图层 2.三板斧(HttpResponse对象) 4.HttpResponse() 5.render() 6.redirect() 7.也可以是一个完整的URL 二:三板斧本质 1.Dja ...

  3. (admin.E108) The value of 'list_display[0]' refers to 'productname', which is not a callable, an attribute of 'ProductAdmin', or an attribute or method on 'product.Product'.

    models.py # 创建产品表 class Product(models.Model): productName = models.CharField('产品名称', max_length=64) ...

  4. 3、数组、集合、Lambda、Stream与Optional类

    一.数组: 数组保存在JVM堆内存中 1.数组的创建: (1).一维数组创建方式一: //一维数组方式一 Integer[] array01 = {1,2,3}; System.out.println ...

  5. IdentityServer4 - v4.x .Net中的实践应用

    认证授权服务的创建 以下内容以密码授权方式为例. 创建模拟访问DB各数据源类 为模拟测试准备的数据源. /// 假设的用户模型 public class TestUser { public strin ...

  6. [C++]bitwise和memberwise的区别

    在看<深入探索C++对象模型>这本书的时候,我看见了bitwise senimatics和memberwise senimatics,看的时候还不清楚这两个是什么意思,书本上直接使用的是英 ...

  7. 均有商业公司支持!2023再看数据湖 hudi iceberg delta2 社区发展现状!

    开源数据湖三剑客 Apache hudi.Apache iceberg .Databricks delta 近年来大动作不断. 2021年8月,Apache Iceberg 的创始人 Ryan Blu ...

  8. 小项目中vuex使用频率不太多我们完全可以用provide inject 来代替可以让项目小不少

    在一般小型项目中vuex实在是太浪费了所以我们可以用到 vue中的provide inject 代替 1.在vue3中我们先另起一个 文件创建一个全局的状态和方法的地方(如果你的全局状态特别的多记得要 ...

  9. Echarts 折线图Demo调色12种,可以直接使用~~~

    测试Demo代码~~ <template> <div> <div id="myChart" ref="mapBox" style= ...

  10. Typora 最后一个免费版本

    介绍 Typora 是一款轻量级的 Markdown 编辑器,其最为出众的特点是: 所见即所得. Typora 于2021年11月23日推出了第一个正式版,并转为收费.不过价格也算合理,89元/3台设 ...