Skip to main content

2294.js

/** ------------------------------------------------------------------------------
*
* 2294. Partition Array Such That Maximum Difference Is K
* Topics: Greedy, Sorting
* https://leetcode.com/problems/partition-array-such-that-maximum-difference-is-k/description/
*
------------------------------------------------------------------------------ */
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var partitionArray = function (nums, k) {
nums.sort((a, b) => a - b)

let ans = 1
let x = nums[0]

for (let i = 1; i < nums.length; i++) {
if (nums[i] - x > k) {
x = nums[i]
ans++
}
}

return ans
}

console.log()