LeetCode solutions

3510. Maximize the Total Height of Unique Towers

My accepted Python solution to LeetCode problem 3510, Maximize the Total Height of Unique Towers, running in 219ms.

  • Difficulty: Medium
  • Python
  • Runtime 219ms
  • Memory 31.1MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 219ms, memory 31.1MB, accepted 2026-01-01.

python
class Solution:
    def maximumTotalSum(self, maximumHeight: List[int]) -> int:
        # Sort in descending order
        maximumHeight.sort(reverse=True)
        
        total = 0
        prev_height = float('inf')
        
        for max_h in maximumHeight:
            # The height we assign must be at most max_h and less than prev_height
            height = min(max_h, prev_height - 1)
            
            if height <= 0:
                return -1
            
            total += height
            prev_height = height
        
        return total

Source