2294. Partition Array Such That Maximum Difference Is K
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 74% Topics: Array, Greedy, Sorting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(nlogn)
# Space: O(1)
# sort, greedy
class Solution(object):
def partitionArray(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
nums.sort()
result, prev = 1, 0
for i in xrange(len(nums)):
if nums[i]-nums[prev] <= k:
continue
prev = i
result += 1
return result
Solution from kamyu104/LeetCode-Solutions · MIT