From 62e0c81badd2601085e86d2164d3983292e286e6 Mon Sep 17 00:00:00 2001 From: fxb Date: Wed, 2 Jan 2019 22:46:06 +0800 Subject: [PATCH] =?UTF-8?q?leetcode=E5=88=B7=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...市天际线(Max Increase to Keep City Skyline).md | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 算法/leetcode/Q807 保持城市天际线(Max Increase to Keep City Skyline).md diff --git a/算法/leetcode/Q807 保持城市天际线(Max Increase to Keep City Skyline).md b/算法/leetcode/Q807 保持城市天际线(Max Increase to Keep City Skyline).md new file mode 100644 index 0000000..1f17b32 --- /dev/null +++ b/算法/leetcode/Q807 保持城市天际线(Max Increase to Keep City Skyline).md @@ -0,0 +1,52 @@ +--- +id="2019-01-02-17-15" +title="Q807 保持城市天际线(Max Increase to Keep City Skyline)" +headWord="leetcode刷题,题目详情见点击跳转" +tags=["java", "leetcode","array",'leetcode'] +category="算法" +serie="leetcode刷题" +--- + +### 解析思路 + +  leetcode 中等难度中比较简单的一个,题目描述[看这里](https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/),最开始看题目是比较懵逼的,第一遍没有看懂,多看几次才明白,其实就是让每个点增大到不超过本行且本列的最大值,输出增加的值的和。so 解题方法就是算出每行每列的最大值,然后每个点与当前行当前列最大值中较小的一个差值的和就是结果。 + +### 代码(Java实现) + +```java +class Solution { + public int maxIncreaseKeepingSkyline(int[][] grid) { + int rowLength = grid.length; + int columnLength = grid[0].length; + //每行对应的最大值 + int[] rowMax = new int[rowLength]; + //每列对应的最大值 + Arrays.fill(rowMax, -1); + int[] columnMax = new int[columnLength]; + Arrays.fill(columnMax, -1); + int sum = 0; + for (int i = 0; i < rowLength; i++) { + for (int j = 0; j < columnLength; j++) { + if (rowMax[i] == -1) { + //计算行最大值 + for (int temp = 0; temp < columnLength; temp++) { + if (rowMax[i] < grid[i][temp]) { + rowMax[i] = grid[i][temp]; + } + } + } + if (columnMax[j] == -1) { + //计算列最大值 + for (int temp = 0; temp < rowLength; temp++) { + if (columnMax[j] < grid[temp][j]) { + columnMax[j] = grid[temp][j]; + } + } + } + sum += Math.min(rowMax[i], columnMax[j]) - grid[i][j]; + } + } + return sum; + } +} +```