Source: https://leetcode.com/problems/sliding-window-maximum/

You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.

/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
// Brute force approach
// O(Nk) time, O(N) space for output
// Keep moving a sliding window of size k and adding to output
var maxSlidingWindow = function(nums, k) {
const output = Array.from({ length: nums.length - k + 1 }, () => -Infinity);
for (let i = 0; i < nums.length - k + 1; i++) {
let max = -Infinity;
for (let j = i; j < i + k; j++) {
max = Math.max(nums[j], max);
}
output[i] = max;
}
return output;
};