欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Leetcode 3301. Maximize the Total Height of Unique Towers

最编程 2024-09-30 13:09:05
...
  • Leetcode 3301. Maximize the Total Height of Unique Towers
    • 1. 解题思路
    • 2. 代码实现
  • 题目链接: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。