[Swift]LeetCode807. 保持城市天際線 | Max Increase to Keep City Skyline

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-ckprfrwo-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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. git

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.github

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.

在二維數組grid中,grid[i][j]表明位於某處的建築物的高度。 咱們被容許增長任何數量(不一樣建築物的數量可能不一樣)的建築物的高度。 高度 0 也被認爲是建築物。app

最後,重新數組的全部四個方向(即頂部,底部,左側和右側)觀看的「天際線」必須與原始數組的天際線相同。 城市的天際線是從遠處觀看時,由全部建築物造成的矩形的外部輪廓。 請看下面的例子。ide

建築物高度能夠增長的最大總和是多少?ui

例子:
輸入: 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] ]

說明:spa

  • 1 < grid.length = grid[0].length <= 50
  •  grid[i][j] 的高度範圍是: [0, 100]
  • 一座建築物佔據一個grid[i][j]:換言之,它們是 1 x 1 x grid[i][j] 的長方體。

Runtime: 44 ms
Memory Usage: 18.4 MB
 1 class Solution {
 2     func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {        
 3         if grid.count <= 1 {
 4             return 0
 5         }
 6         
 7         let count = grid.count
 8         
 9         var hHeight:[Int] = Array(repeating: 0 , count: count)
10         var vHeight:[Int] = Array(repeating: 0 , count: count)        
11         
12         var height : Int = 0
13         
14         for hIdx in 0..<count{
15             for vIdx in 0..<count {
16                 height = grid[hIdx][vIdx]
17                 if height > hHeight[hIdx] {
18                     hHeight[hIdx] = height
19                 }
20                 if height > vHeight[vIdx] {
21                     vHeight[vIdx] = height
22                 }
23             }
24         }        
25 
26         var gridOffsetCount:Int = 0
27         var maxGrid : Int = 0
28         for vIdx in 0..<count{
29             for hIdx in 0..<count {
30                 maxGrid = min(vHeight[vIdx] , hHeight[hIdx])
31                 height = grid[hIdx][vIdx]
32                 if maxGrid > height{
33                     gridOffsetCount += maxGrid - height
34                 }                
35             }
36         }        
37         return gridOffsetCount
38     }
39 }

44mscode

 1 class Solution {
 2     func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {
 3         let side = grid.compactMap{ $0.max() }
 4         let top = getTopSkyline(for: grid)
 5         
 6         guard grid.count > 0,
 7             top.count == side.count else {
 8                 // invalid grid input
 9                 return 0
10             }
11         
12         let increase = computeMaxIncrease(for: grid, top: top, side:side)
13         return increase
14     }
15     
16     private func computeMaxIncrease(for grid:[[Int]], top: [Int], side:[Int]) -> Int {
17         var increase = 0
18         for i in 0..<top.count {
19             for j in 0..<side.count {
20                 let topMax = top[i]
21                 let sideMax = side[j]
22                 let highest = min(top[i], side[j])
23                 
24                 increase += highest - grid[i][j]
25             }
26         }
27         
28         return increase
29     }
30     
31     private func getTopSkyline(for grid:[[Int]]) -> [Int] {
32         var top = [Int]()
33         let length = grid.count
34         for i in 0..<length {
35             var maxValue = -1
36 
37             for j in 0..<length {
38                 maxValue = max(grid[j][i], maxValue)
39             }
40             top.append(maxValue)
41         }
42         
43         return top
44     }
45 }

Runtime: 48 ms
Memory Usage: 19.1 MB
 1 class Solution {
 2     func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {
 3         var m:Int = grid.count
 4         var n:Int = grid[0].count
 5         var res:Int = 0
 6         var row:[Int] = [Int](repeating:0,count:m)
 7         var col:[Int] = [Int](repeating:0,count:n)
 8         for i in 0..<m
 9         {
10             for j in 0..<n
11             {
12                 row[i] = max(row[i], grid[i][j])
13                 col[j] = max(col[j], grid[i][j])
14             }
15         }
16         for i in 0..<m
17         {
18             for j in 0..<n
19             {
20                 res += min(row[i] - grid[i][j], col[j] - grid[i][j])
21             }
22         }
23         return res
24     }
25 }

48ms

 1 class Solution {
 2     func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {
 3         let maxRow = grid.map { $0.max()! }
 4         let maxCol = getMaxCol(grid)
 5         let length = grid.count
 6         var out = 0
 7         
 8         for i in 0..<length {
 9             for j in 0..<length {
10                 let increase = min(maxRow[i], maxCol[j])
11                 let diff = increase - grid[i][j]
12                 out += diff
13             }
14         }
15         
16         return out
17         
18     }
19 
20     func getMaxCol(_ grid: [[Int]]) -> [Int] {
21         var out = [Int]()
22         for column in 0 ..< grid.count {
23             out.append(grid.map { $0[ column ] }.max()!)
24         }
25         return out
26     }
27 }

52ms

 1 class Solution {
 2     func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {
 3         let row_max = grid.map { $0.max()! }
 4         let col_max = (0..<grid[0].count).map { i in grid.map { $0[i] }.max()! }
 5         var count = 0
 6         for (i, row) in grid.enumerated() {
 7             for (j, el) in row.enumerated() {
 8                 count += min(row_max[i], col_max[j]) - el
 9             }
10         }
11         return count
12     }
13 }

56ms

 1 class Solution {
 2     func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {
 3         let row_maxes = grid.map { $0.max()! }
 4         let col_maxes = (0..<grid[0].count)
 5             .map { i in grid.reduce(0, { prev, next -> Int in max(prev, next[i]) }) }
 6 
 7         return zip(row_maxes, grid)
 8             .map { row_max, row in zip(col_maxes, row)
 9             .reduce(0, { prev, vals -> Int in prev + min(row_max, vals.0) - vals.1 }) }
10             .reduce(0, +)
11     }
12 }

64ms

 1 class Solution {
 2     func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {
 3         var result : Int = 0
 4         var rowMax :[Int] = []
 5         var colMax :[Int] = []
 6         for i in 0 ..< grid.count {
 7             rowMax.append(grid[i].max()!)
 8             colMax.append(grid.map{ $0[i] }.max()!)
 9         }
10         
11         for i in 0 ..< grid.count {
12             for j in 0 ..< grid.count {
13                 result += min(rowMax[i],colMax[j]) - grid[i][j]
14             }
15         }
16         return result
17     }
18 }

72ms

 1 class Solution {
 2     func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {
 3     let rowsMaxes = grid.map { $0.max() ?? 0 }
 4     let columnsMaxes = getVerticalView(of: grid).map { $0.max() ?? 0 }
 5     
 6     return grid.enumerated().map { (arg) -> Int in
 7         
 8         let (i, _) = arg
 9         return grid[i].enumerated().map { (j, _) -> Int in
10             let newTotal = min(rowsMaxes[i], columnsMaxes[j])
11             return newTotal - grid[i][j]
12         }.reduce(0, +)
13     }.reduce(0, +)
14 }
15 
16 fileprivate func getVerticalView(of grid: [[Int]]) -> [[Int]] {
17     return (0..<grid[0].count).map { index -> [Int] in
18         return grid.map { $0[index] }
19     }
20   } 
21 }

76ms

 1 class Solution {
 2     func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {
 3         var horizontalSkyline = [Int:Int]()
 4         var verticalSkyline = [Int:Int]()
 5         for (hIndex, row) in grid.enumerated() {
 6             var skyline = 0
 7             for (vIndex, element) in row.enumerated() {
 8                 skyline = max(skyline, element)
 9                 verticalSkyline[vIndex] = max(verticalSkyline[vIndex] ?? 0, element)
10             }
11             horizontalSkyline[hIndex] = skyline 
12         }
13         var sum = 0
14         for (hIndex, row) in grid.enumerated() {
15             for (vIndex, element) in row.enumerated() {
16                 sum += min(verticalSkyline[vIndex] ?? 0, horizontalSkyline[hIndex] ?? 0) - element
17             }
18         }
19         return sum
20     }
21 }
相關文章
相關標籤/搜索