LeetCode solutions

3620. Maximum Number of Distinct Elements After Operations

My accepted Python solution to LeetCode problem 3620, Maximum Number of Distinct Elements After Operations, running in 751ms.

  • Difficulty: Medium
  • Python
  • Runtime 751ms
  • Memory 30.9MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 751ms, memory 30.9MB, accepted 2026-01-01.

python
class Solution:
    def maxDistinctElements(self, nums: List[int], k: int) -> int:
        nums.sort()
        n = len(nums)
        
        # Greedily assign the smallest possible value to each element
        # Each nums[i] can become nums[i] - k to nums[i] + k
        
        prev = float('-inf')  # Last value we assigned
        distinct = 0
        
        for num in nums:
            # The range for this number is [num - k, num + k]
            # We want to pick the smallest value > prev
            low = num - k
            high = num + k
            
            # The value we assign must be > prev and in [low, high]
            # Pick max(prev + 1, low) if it's <= high
            val = max(prev + 1, low)
            
            if val <= high:
                prev = val
                distinct += 1
            # If val > high, we can't assign a distinct value
        
        return distinct

Source