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; +}