2294. Partition Array Such That Maximum Difference Is K

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2294

class Solution:
    def partitionArray(self, nums: List[int], k: int) -> int:
        nums.sort()
        res, a = 1, nums[0]
        for b in nums:
            if b - a > k:
                a = b
                res += 1
        return res