1. Sliding Window Maximum

Given an array 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.

Follow up:

Could you solve it in linear time?

Example:

Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7]
Explanation: Window position Max
--------------- -----
[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

很奇怪很多答案为什么又是堆又是哈希表的。。。

用一个tmp_max变量记录窗口里的最大值,向右移动时,如果最左边出去的是最大值tmp_max,就在窗口里重新线性搜索或者调用max_element()或者其他什么方法找到最大值,右端点进来的新的值和tmp_max比较一下取大的作为新的tmp_max

class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int l = 0, r = k;
vector<int>ans;
int tmp = INT_MIN;
for(int i = 0; i < r; ++i)tmp = max(nums[i], tmp);
ans.push_back(tmp);
r++;
l++;
while(r <= nums.size()){
if(tmp == nums[l-1]){
tmp = INT_MIN;
for(int i = l; i < r; ++i)tmp = max(tmp, nums[i]);
}else{
tmp = max(tmp, nums[r-1]);
}
ans.push_back(tmp);
l++;
r++;
}
return ans;
}
};

【刷题-LeetCode】239. Sliding Window Maximum的更多相关文章

  1. [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  2. [leetcode]239. Sliding Window Maximum滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  3. leetcode 239 Sliding Window Maximum

    这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...

  4. [leetcode] #239 Sliding Window Maximum (Hard)

    原题链接 题意: 给定一个数组数字,有一个大小为k的滑动窗口,它从数组的最左边移动到最右边.你只能在窗口看到k个数字.每次滑动窗口向右移动一个位置. 记录每一次窗口内的最大值,返回记录的值. 思路: ...

  5. 【LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum   Given an array nums, there is a sliding window of size k which is moving fr ...

  6. 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...

  7. 239. Sliding Window Maximum

    题目: Given an array nums, there is a sliding window of size k which is moving from the very left of t ...

  8. LeetCode题解-----Sliding Window Maximum

    题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...

  9. 239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

随机推荐

  1. CF289B Polo the Penguin and Matrix 题解

    Content 有一个 \(n\times m\) 的矩阵 \(A\),每次操作你可以将某一个元素增加或减少 \(d\),求是所有元素相等的最小操作次数,或者不存在这样的操作方案. 数据范围:\(1\ ...

  2. mail如何在linux中发送邮件,使用163邮箱发信。

    如何在linux中发送邮件,使用163邮箱发信.   linux中,可以使用mail命令往外发送邮件,在使用前,只需要指定如下简单配置即可,这里演示用  163.com    邮箱发送至 qq.com ...

  3. vue+uniapp实现照录像,相册选择 | 图片裁剪压缩,视频压缩

    一.插件简介 Zhimi-Camera(智密 - 智密 - 相机图册插件-视频/图片选择器)是一个支持拍照,录像,相册选择功能,自带图片裁剪,图片压缩,视频压缩,选择数量限制的uniapp原生插件.平 ...

  4. JAVA通过身份证号码获取出生日期、年龄、性别

    JAVA验证身份证号码是否正确:https://www.cnblogs.com/pxblog/p/12038278.html /** * 通过身份证号码获取出生日期(birthday).年龄(age) ...

  5. C++之去重

    note 今天刷题,忘了去重复库函数,于是手写了一个. 前提: 必须保证数组是有序的. 源码 template <typename T> void unique_arr(T arr[], ...

  6. 1479 小Y的数论题

    小Y喜欢研究数论,并且喜欢提一些奇怪的问题.这天他找了三个两两互质的数a, b, c,以及另一个数m, 现在他希望找到三个(0, m)范围内的整数x, y, z,使得 (xa+yb) Mod m=(z ...

  7. 第八个知识点:交互式的定义如何帮助计算和IP类问题是什么

    第八个知识点:交互式的定义如何帮助计算和IP类问题是什么 这是系列中的第8篇,我们主要讨论计算中交互作用的用处和IP类问题是什么. 为了回答这些问题,我们首先给交互式证明系统一个简洁的介绍.众所周知, ...

  8. 大数据分布式存储之Cassandra

    分布式存储区别于集中式数据库存储,通过网络将海量数据存储到企业的各个数据节点(可能分布到不同的数据中心或机架上): 分布式存储需要考虑的问题 元数据管理 元数据是指数据本身的标识,通过元数据能很快的找 ...

  9. linux系统安装java

    1.下载Java压缩包 *.gz 2.解压 3.修改Linux配置文件,配置Java环境变量 4.使用命令source /etc/profile让修改生效 转载 https://www.cnblogs ...

  10. MySQL基础操作指南

    启动停止MySQL服务 1.Windows服务窗口启动:开始 → 运行 → services.msc → 找到MySQL 56服务 2.MySQL命令启动:开始 → 运行 → 管理员权限运行cmd → ...