LeetCode solutions

2664. Maximize Greatness of an Array

My accepted Python solution to LeetCode problem 2664, Maximize Greatness of an Array, running in 34ms.

  • Difficulty: Medium
  • Python
  • Runtime 34ms
  • Memory 30.7MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 34ms, memory 30.7MB, accepted 2025-12-31.

python
class Solution:
    def maximizeGreatness(self, nums: List[int]) -> int:
        # Sort and greedily match
        nums.sort()
        greatness = 0
        j = 0  # Pointer to elements being beaten
        
        for i in range(len(nums)):
            if nums[i] > nums[j]:
                greatness += 1
                j += 1
        
        return greatness

Source