leecode [保持城市天際線]代碼實現

題目描述  https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/description/

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

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

Python代碼實現以下,若有錯誤遺漏,歡迎指正!

import pandas as pd
import numpy as np


def maxIncreaseKeepingSkyline( grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
nums = grid
nums = np.array(nums).T
top_bottom = []
for i in range(nums.shape[0]):
ls = []
for j in range(nums.shape[1]):
ls.append(nums[i][j])
top_bottom.append(max(ls))

left_right = []
for i in range(nums.shape[1]):
ms = []
for j in range(nums.shape[0]):
ms.append(nums[j][i])
left_right.append(max(ms))
a = [[0 for _ in range(len(left_right))] for _ in range(len(top_bottom))]
a = np.array(a).T
cnt = 0
for i in range(len(left_right)):
for j in range(len(top_bottom)):
aa = min(left_right[i], top_bottom[j])
cnt = cnt + aa - nums[i][j]
a[i][j] = aa
return cnt


if __name__ == '__main__':
grid = [[3, 0, 8, 4], [2, 4, 5, 7], [9, 2, 6, 3], [0, 3, 1, 0]]
res = maxIncreaseKeepingSkyline(grid=grid)
print(res)
相關文章
相關標籤/搜索