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 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 a1 x 1 x grid[i][j]
rectangular prism.
- class Solution {
- public int maxIncreaseKeepingSkyline(int[][] grid) {
- //找到垂直每一行,与水平每一行的最高天际线
- int[] vertical = new int[grid.length];
- int[] horizontal = new int[grid.length];
- for(int i=0;i<grid.length;i++){
- for(int j=0;j<grid.length;j++){
- if(grid[i][j]>vertical[i]){
- vertical[i] = grid[i][j];
- }
- }
- }
- for(int i=0;i<grid.length;i++){
- for(int j=0;j<grid.length;j++){
- if(grid[j][i]>horizontal[i]){
- horizontal[i] = grid[j][i];
- }
- }
- }
- int max = 0;
//遍历一遍,将每一行的最高天际线,与每一列的最高天际线进行比较,最低的值减去矩阵的值总和就是答案。- for(int i=0;i<grid.length;i++){
- for(int j=0;j<grid.length;j++){
- max += horizontal[j]>vertical[i]?vertical[i]-grid[i][j]:horizontal[j]-grid[i][j];
- }
- }
- return max;
- }
- }
Leetcode 807 Max Increase to Keep City Skyline 不变天际线的更多相关文章
- 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 ...
- Leetcode 807. Max Increase to Keep City Skyline
class Solution(object): def maxIncreaseKeepingSkyline(self, grid): """ :type grid: Li ...
- LC 807. 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 ...
- 【LeetCode】807. Max Increase to Keep City Skyline 解题报告(Python &C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode&Python] Problem 807. 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 ...
- 【Leetcode】807. Max Increase to Keep City Skyline
Description In a 2 dimensional array grid, each value grid[i][j] represents the height of a building ...
- [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 ...
- [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 ...
- BZOJ1628: [Usaco2007 Demo]City skyline
1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 256 Solved: 210[Su ...
随机推荐
- 脚本语言丨Batch入门教程第四章:调用与传参
今天是Batch入门教程的最后一章内容:调用与传参.相信通过前面的学习,大家已经掌握了Windows Batch有关的基础知识和编程方法,以及利用Windows Batch建立初级的编程思维方式.今后 ...
- [Swift]LeetCode76. 最小覆盖子串 | Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [Swift]LeetCode653. 两数之和 IV - 输入 BST | Two Sum IV - Input is a BST
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- redis缓存使用
什么是缓存(cache): 在项目中没有必要每次请求都查询数据库的情况就可以使用缓存,让每次请求先查询缓存,如果命中,就直接返回缓存结果,如果没有命中,就查询数据库, 并将查询结果放入缓存,下次请求时 ...
- WebWorker与WebSocket实现前端消息总线
Web Worker让JS有了多线程的能力,可以将复杂耗时的操作都交付给Worker线程处理.WebSocket让web端与服务端维持一个有效的长连接,实现服务端主动推送数据.将二者一结合,业务系统信 ...
- 一步一步用Canvas写一个贪吃蛇
之前在慕课网看了几集Canvas的视频,一直想着写点东西练练手.感觉贪吃蛇算是比较简单的了,当年大学的时候还写过C语言字符版的,没想到还是遇到了很多问题. 最终效果如下(图太大的话 时间太长 录制gi ...
- centos7中安装、配置、验证、卸载redis
本文介绍在centos7中安装.配置.验证.卸载redis等操作,以及在使用redis中的一些注意事项. 一 安装redis 1 创建redis的安装目录 利用以下命令,切换到/usr/local路径 ...
- Java高级开发工程师面试笔记
最近在复习面试相关的知识点,然后做笔记,后期(大概在2018.02.01)会分享给大家,尽自己最大的努力做到最好,还希望到时候大家能给予建议和补充 ----------------2018.03.05 ...
- Latex文件分别用Texwork和Winedt打开时,产生中文乱码的解决方法
中文兼容方法(能保证编译成功) \usepackage{CJK} \begin{document} \begin{CJK}{GBK}{kai} ... 中文 ... \end{CJK} \end{do ...
- mybatis中resultMap配置细则
resultMap算是mybatis映射器中最复杂的一个节点了,能够配置的属性较多,我们在mybatis映射器配置细则这篇博客中已经简单介绍过resultMap的配置了,当时我们介绍了resultMa ...