Skip to content
LC-2779 Medium LeetCode

2779. Maximum Beauty of an Array After Applying Operation

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 58% Topics: Array, Binary Search, Sliding Window, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(1)

# sort, two pointers, sliding window
class Solution(object):
    def maximumBeauty(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        nums.sort()
        left = 0
        for right in xrange(len(nums)):
            if nums[right]-nums[left] > k*2:
                left += 1
        return right-left+1

Solution from kamyu104/LeetCode-Solutions · MIT