diff --git a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README.md b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README.md index bbcd842bc17c7..ee4861ecabbfa 100644 --- a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README.md +++ b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README.md @@ -177,4 +177,32 @@ function partitionArray(nums: number[], k: number): number { + + +### Solution 2 + + + +#### TypeScript + +```ts +function partitionArray(nums: number[], k: number): number { + if (k === 0) return new Set(nums).size; + + let c = 0; + + while (nums.length) { + const max = Math.max(...nums) - k; + nums = nums.filter(x => x < max); + c++; + } + + return c; +} +``` + + + + + diff --git a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README_EN.md b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README_EN.md index bbf4c0002ffcb..cc8121ad3c102 100644 --- a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README_EN.md +++ b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README_EN.md @@ -171,4 +171,32 @@ function partitionArray(nums: number[], k: number): number { + + +### Solution 2 + + + +#### TypeScript + +```ts +function partitionArray(nums: number[], k: number): number { + if (k === 0) return new Set(nums).size; + + let c = 0; + + while (nums.length) { + const max = Math.max(...nums) - k; + nums = nums.filter(x => x < max); + c++; + } + + return c; +} +``` + + + + + diff --git a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/Solution2.ts b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/Solution2.ts new file mode 100644 index 0000000000000..097a1f8e79388 --- /dev/null +++ b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/Solution2.ts @@ -0,0 +1,13 @@ +function partitionArray(nums: number[], k: number): number { + if (k === 0) return new Set(nums).size; + + let c = 0; + + while (nums.length) { + const max = Math.max(...nums) - k; + nums = nums.filter(x => x < max); + c++; + } + + return c; +}