Divide Array Into Arrays With Max Difference
LeetCode題目: 2966. Divide Array Into Arrays With Max Difference
My solution:
/**
* @param {number[]} nums
* @param {number} k
* @return {number[][]}
*/
const divideArray = (nums, k) => {
const newArr = [];
nums.sort((a, b) => a - b);
for(let i=0; i<nums.length-2; i+=3) {
if(nums[i+2] - nums[i] > k) return [];
newArr.push([nums[i], nums[i+1], nums[i+2]]);
}
return newArr
};