{"size": 1268, "ext": "js", "max_stars_repo_path": "2021/01/02/DailyQuestion/question.js", "max_stars_repo_name": "DodenMaki/CodeDaily", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "2021/01/02/DailyQuestion/question.js", "max_issues_repo_name": "DodenMaki/CodeDaily", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "2021/01/02/DailyQuestion/question.js", "max_forks_repo_name": "DodenMaki/CodeDaily", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 18.9253731343, "max_line_length": 82, "alphanum_fraction": 0.4739747634}
/**
 * 239.滑动窗口最大值
 * LeetCode：https://leetcode-cn.com/problems/sliding-window-maximum/
 */

/**
 * 给你一个整数数组 nums，有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
 * 返回滑动窗口中的最大值。
 * 
 * 示例 1：
 * 输入：nums = [1,3,-1,-3,5,3,6,7], k = 3
 * 输出：[3,3,5,5,6,7]
 * 解释：
 *   滑动窗口的位置          最大值
 * ---------------         -----
 * [1 3 -1] -3 5 3 6 7       3
 * 1 [3 -1 -3] 5 3 6 7       3
 * 1 3 [-1 -3 5] 3 6 7       5
 * 1 3 -1 [-3 5 3] 6 7       5
 * 1 3 -1 -3 [5 3 6] 7       6
 * 1 3 -1 -3 5 [3 6 7]      7
 * 示例 2：
 * 输入：nums = [1], k = 1
 * 输出：[1]
 * 示例 3：
 * 输入：nums = [1,-1], k = 1
 * 输出：[1,-1]
 * 示例 4：
 * 输入：nums = [9,11], k = 2
 * 输出：[11]
 * 示例 5：
 * 输入：nums = [4,-2], k = 2
 * 输出：[4]
 */

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var maxSlidingWindow = function(nums, k) {
  var len = nums.length;
  var result = [];
  var deQueue = [];

  for (var i = 0; i < len; i++) {
    while (deQueue.length && nums[deQueue[deQueue.length - 1]] < nums[i]) {
      deQueue.pop();
    }

    deQueue.push(i);

    while (deQueue.length && deQueue[0] <= i - k) {
      deQueue.shift();
    }

    if (i >= k - 1) {
      result.push(nums[deQueue[0]]);
    }
  }

  return result;
};

module.exports = {
  maxSlidingWindow,
}