xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

LeetCode 滑动窗口最大值

LeetCode 滑动窗口最大值

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var maxSlidingWindow = function(nums, k) {
  // slide window
  // nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
  const result = [];
  const len = nums.length;
  for(let i = 0; i < len - k + 1; i++) {
    // copy
    let arr = nums.slice(i, k + i);
    let tempMax = arr.reduce((max, item) => max <= item ? item : max, -1/0);
    result.push(tempMax);
  }
  return result;
};

超出时间限制 bug

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-11-16
 * @modified
 *
 * @description
 * @difficulty Hard
 * @complexity O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/sliding-window-maximum/
 * @link https://leetcode-cn.com/problems/sliding-window-maximum/
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;


/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var maxSlidingWindow = function(nums, k) {
  // slide window
  // nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
  const result = [];
  const len = nums.length;
  for(let i = 0; i < len - k + 1; i++) {
    // copy
    let arr = nums.slice(i, k + i);
    log(`arr =`, arr, i)
    let tempMax = arr.reduce((max, item) => max <= item ? item : max, -1/0);
    log(`max =`, tempMax)
    result.push(tempMax);
  }
  log(`result =`, result)
  return result;
};

const nums = [1,3,-1,-3,5,3,6,7], k = 3;
const test = maxSlidingWindow(nums, k);

// 输出: [3,3,5,5,6,7]


/*

https://leetcode-cn.com/submissions/detail/123944499/testcase/


超出时间限制


*/


/*


提示:

1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
1 <= k <= nums.length

*/


solutions

递归

动态规划



refs

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

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



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-11-17 07:22  xgqfrms  阅读(112)  评论(1编辑  收藏  举报