Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)_
- [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)_
Original file line number Diff line number Diff line change
@@ -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);
});
});
37 changes: 37 additions & 0 deletions src/neetcode/sliding-window/sliding-window-maximum/solution.ts
Original file line number Diff line number Diff line change
@@ -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<number>(n);
const rightMax = new Array<number>(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<number>(n - k + 1);

for (let i = 0; i < n - k + 1; i++) {
output[i] = Math.max(leftMax[i + k - 1], rightMax[i]);
}

return output;
};
Loading