Leetcode 3301. Maximize the Total Height of Unique Towers
1. 解题思路
这一题思路上还是比较直接的,我们只需要排序之后从大到小依次分配最大可能的高度即可。
如果出现某个位置最大允许分配的高度为0,那么就说明无法构造成功,反之即可给出最大高度了。
2. 代码实现
给出python代码实现如下:
class Solution:
def maximumTotalSum(self, maximumHeight: List[int]) -> int:
maximumHeight = sorted(maximumHeight, reverse=True)
_max = math.inf
ans = 0
for h in maximumHeight:
if _max <= 1:
return -1
ans += min(h, _max-1)
_max = min(h, _max-1)
return ans
提交代码评测得到:耗时1081ms,占用内存31.5MB。
原文地址:https://blog.csdn.net/codename_cys/article/details/142637699
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!