From 0a8b01c1ae5965712a5b2c281af32d66925c5e17 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sat, 16 Nov 2024 19:53:22 +0300 Subject: [PATCH] feat: add solutions to lc problem: No.3254 --- .../README.md | 46 +++++++++++++++++++ .../README_EN.md | 46 +++++++++++++++++++ .../Solution2.js | 13 ++++++ .../Solution2.ts | 13 ++++++ 4 files changed, 118 insertions(+) create mode 100644 solution/3200-3299/3254.Find the Power of K-Size Subarrays I/Solution2.js create mode 100644 solution/3200-3299/3254.Find the Power of K-Size Subarrays I/Solution2.ts diff --git a/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/README.md b/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/README.md index 05edba1805f28..6588c427f3ce3 100644 --- a/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/README.md +++ b/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/README.md @@ -204,4 +204,50 @@ function resultsArray(nums: number[], k: number): number[] { + + +### Solution 2 + + + +#### TypeScript + +```ts +export function resultsArray(nums: number[], k: number): number[] { + const n = nums.length; + const ans: number[] = []; + + for (let i = 0, j = 0; i < n; i++) { + if (i && nums[i - 1] + 1 !== nums[i]) j = i; + if (i >= k - 1) { + ans.push(i - k + 1 < j ? -1 : nums[i]); + } + } + + return ans; +} +``` + +#### JavaScript + +```js +export function resultsArray(nums, k) { + const n = nums.length; + const ans = []; + + for (let i = 0, j = 0; i < n; i++) { + if (i && nums[i - 1] + 1 !== nums[i]) j = i; + if (i >= k - 1) { + ans.push(i - k + 1 < j ? -1 : nums[i]); + } + } + + return ans; +} +``` + + + + + diff --git a/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/README_EN.md b/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/README_EN.md index 4c2a7c8a7d69c..e1dbb3e64a52d 100644 --- a/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/README_EN.md +++ b/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/README_EN.md @@ -202,4 +202,50 @@ function resultsArray(nums: number[], k: number): number[] { + + +### Solution 2 + + + +#### TypeScript + +```ts +export function resultsArray(nums: number[], k: number): number[] { + const n = nums.length; + const ans: number[] = []; + + for (let i = 0, j = 0; i < n; i++) { + if (i && nums[i - 1] + 1 !== nums[i]) j = i; + if (i >= k - 1) { + ans.push(i - k + 1 < j ? -1 : nums[i]); + } + } + + return ans; +} +``` + +#### JavaScript + +```js +export function resultsArray(nums, k) { + const n = nums.length; + const ans = []; + + for (let i = 0, j = 0; i < n; i++) { + if (i && nums[i - 1] + 1 !== nums[i]) j = i; + if (i >= k - 1) { + ans.push(i - k + 1 < j ? -1 : nums[i]); + } + } + + return ans; +} +``` + + + + + diff --git a/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/Solution2.js b/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/Solution2.js new file mode 100644 index 0000000000000..8546a336b709b --- /dev/null +++ b/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/Solution2.js @@ -0,0 +1,13 @@ +export function resultsArray(nums, k) { + const n = nums.length; + const ans = []; + + for (let i = 0, j = 0; i < n; i++) { + if (i && nums[i - 1] + 1 !== nums[i]) j = i; + if (i >= k - 1) { + ans.push(i - k + 1 < j ? -1 : nums[i]); + } + } + + return ans; +} diff --git a/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/Solution2.ts b/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/Solution2.ts new file mode 100644 index 0000000000000..3b3656a4b082f --- /dev/null +++ b/solution/3200-3299/3254.Find the Power of K-Size Subarrays I/Solution2.ts @@ -0,0 +1,13 @@ +export function resultsArray(nums: number[], k: number): number[] { + const n = nums.length; + const ans: number[] = []; + + for (let i = 0, j = 0; i < n; i++) { + if (i && nums[i - 1] + 1 !== nums[i]) j = i; + if (i >= k - 1) { + ans.push(i - k + 1 < j ? -1 : nums[i]); + } + } + + return ans; +}