2294. Partition Array Such That Maximum Difference Is K

2294. Partition Array Such That Maximum Difference Is K

這個題目屬於 Greedy 的題目,最困難的地方就是可以知道這是 Greedy 的題目。

class Solution:
    def partitionArray(self, nums: List[int], k: int) -> int:
        
        nums.sort()

        count = 0

        slow = 0
        fast = 0

        count = 1
        while fast < len(nums):
            if nums[fast] - nums[slow] > k:
                slow = fast
                count += 1
            fast += 1
        
        return count