diff --git a/README.md b/README.md index 1923b73..3a96a81 100644 --- a/README.md +++ b/README.md @@ -90,4 +90,5 @@ - [Task #16](src/neetcode/sliding-window/longest-substring-without-repeating-characters) – _Longest Substring Without Repeating Characters_ - _[check the task](https://neetcode.io/problems/longest-substring-without-duplicates)_ - [Task #17](src/neetcode/sliding-window/longest-repeating-character-replacement) – _Longest Repeating Character Replacement_ - _[check the task](https://neetcode.io/problems/longest-repeating-substring-with-replacement)_ - [Task #17](src/neetcode/sliding-window/longest-repeating-character-replacement) – _Longest Repeating Character Replacement_ - _[check the task](https://neetcode.io/problems/longest-repeating-substring-with-replacement)_ - - [Task #19](src/neetcode/sliding-window/minimum-window-substring) – _Minimum Window Substring_ - _[check the task](https://neetcode.io/problems/minimum-window-with-characters)_ \ No newline at end of file + - [Task #19](src/neetcode/sliding-window/minimum-window-substring) – _Minimum Window Substring_ - _[check the task](https://neetcode.io/problems/minimum-window-with-characters)_ + - [Task #20](src/neetcode/sliding-window/sliding-window-maximum) – _Sliding Window Maximum_ - _[check the task](https://neetcode.io/problems/sliding-window-maximum)_ \ No newline at end of file diff --git a/src/neetcode/sliding-window/sliding-window-maximum/solution.test.ts b/src/neetcode/sliding-window/sliding-window-maximum/solution.test.ts new file mode 100644 index 0000000..dd72a4b --- /dev/null +++ b/src/neetcode/sliding-window/sliding-window-maximum/solution.test.ts @@ -0,0 +1,11 @@ +import { maxSlidingWindow } from './solution'; + +describe('Sliding Window Maximum | NeetCode | RoadMap | Testcases', () => { + test('#1 Example', () => { + const nums = [1, 2, 1, 0, 4, 2, 6]; + const k = 3; + const expected = [2, 2, 4, 4, 6]; + const output = maxSlidingWindow(nums, k); + expect(output).toEqual(expected); + }); +}); diff --git a/src/neetcode/sliding-window/sliding-window-maximum/solution.ts b/src/neetcode/sliding-window/sliding-window-maximum/solution.ts new file mode 100644 index 0000000..68074b1 --- /dev/null +++ b/src/neetcode/sliding-window/sliding-window-maximum/solution.ts @@ -0,0 +1,37 @@ +/** + * @param {number[]} nums - an array of integers + * @param {number} k - integer + * @return {number[]} a list that contains the maximum element in the sliding window at each step. + * The window of size `k` starts at the left edge of the array slides one position to the right until + * it reaches the right edge of the array. + */ +export const maxSlidingWindow = (nums: number[], k: number): number[] => { + const n = nums.length; + const leftMax = new Array(n); + const rightMax = new Array(n); + + leftMax[0] = nums[0]; + rightMax[n - 1] = nums[n - 1]; + + for (let i = 1; i < n; i++) { + if (i % k === 0) { + leftMax[i] = nums[i]; + } else { + leftMax[i] = Math.max(leftMax[i - 1], nums[i]); + } + + if ((n - 1 - i) % k === 0) { + rightMax[n - 1 - i] = nums[n - 1 - i]; + } else { + rightMax[n - 1 - i] = Math.max(rightMax[n - i], nums[n - 1 - i]); + } + } + + const output = new Array(n - k + 1); + + for (let i = 0; i < n - k + 1; i++) { + output[i] = Math.max(leftMax[i + k - 1], rightMax[i]); + } + + return output; +};