题解 | #滑动窗口的最大值#

滑动窗口的最大值

http://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788

function maxInWindows(num, size)
{
    // write code here
    let len = num.length
    if (len < size || size === 0) return ''
    let q = []
    let res = []
    for (let i = 0; i < len; i++) {
        while (q.length && num[i] >= num[q[q.length - 1]]) {
            q.pop()
        }
        q.push(i)
        while (q[0] <= i - size) {
            q.shift()
        }
        if (i >= size - 1) res.push(num[q[0]])
    }
    return res
}
module.exports = {
    maxInWindows : maxInWindows
};
全部评论

相关推荐

点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务