class Solution:
def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
nums.sort()
res = []
for i in range(0, len(nums), 3):
if nums[i + 2] - nums[i] > k:
return []
res.append(nums[i: i + 3])
return resclass Solution:
def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
max_num = max(nums)
count = [0] * (max_num + 1)
for num in nums:
count[num] += 1
res = []
group = []
for num in range(max_num + 1):
while count[num] > 0:
group.append(num)
count[num] -= 1
if len(group) == 3:
if group[2] - group[0] > k:
return []
res.append(group)
group = []
return resWhere is the size of the array and is the maximum element in .