this is not what
const a = {}
let b = () => {}
this is todo2
翻着我们的照片
想念若隐若现,去年的冬天,我们笑得很甜。
todo2-1
this is the content of todo2-1
function maxSlidingWindow(nums, k) {
// window存储当前窗口中数据的下标
const window = [];
// result存储窗口中的最大值
const result = [];
for (let i = 0; i < nums.length; i++) {
if (i - window[0] > k - 1) {
// 窗口不断往右移动,当最大值在窗口最左侧,但窗口的长度超出k时的情况,就要把左侧的最大值剔除,比如窗口为【3,-1,-3】,继续往右时,就要把左侧的3剔除
window.shift(); // 剔除窗口长度超出范围时左侧的最大值
}
for (let j = window.length - 1; j >= 0; j--) {
// 当前窗口的值依次和要插入的值做比较,如果小于要插入的值,剔除掉该值,直到window为空为止(保证window中最左侧的值为最大值)
if (nums[window[j]] <= nums[i]) {
window.pop();
}
}
// 添加右侧新加入的值,插入新值时有两种情况:
// 1、新值为最大值时,则window此时为空;
// 2、新值不为最大值时,window已剔除掉比新值小的值。
// 始终保证window中最左侧的值为最大值
window.push(i);
if (i >= k - 1) {
// 窗口是从0开始移动,当移动的距离,大于等于目标范围后,以后再往后移动一次,就要写入当前窗口的最大值
result.push(nums[window[0]]);
}
}
return result;
}
console.log(maxSlidingWindow([1, 3, -1, -3, 5, 3, 6, 7], 3)); // [3, 3, 5, 5, 6, 7]
todo2-2
this is the content of todo2-2