leetcode 239 Sliding Window Maximum
这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做:
1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆数组中的位置
2用一个set保存已经被删除的元素索引(这里指的是输入数组索引),这一点可选;还有一种做法就是直接将已删除元素的值设为最小INT_MIN,不过在leetcode里面并没有说清楚数据范围
class Solution {
public:
unordered_map<int, int> dict;
unordered_set<int> invalidSet;
inline int LEFT(int i){
return 2 * i + 1;
}
inline int RIGHT(int i){
return 2 * i + 2;
}
inline int PARENT(int i){
return (i - 1) / 2;
}
void max_heapify(int ind, vector<int>& arr, const vector<int>& value_arr){
int l = LEFT(ind);
int r = RIGHT(ind);
int largest = ind;
if(l < arr.size() && (invalidSet.find(arr[l]) == invalidSet.end())
&& (invalidSet.find(arr[largest]) != invalidSet.end()
|| value_arr[arr[l]] > value_arr[arr[largest]])){
largest = l;
}
if(r < arr.size() && (invalidSet.find(arr[r]) == invalidSet.end())
&& (invalidSet.find(arr[largest]) != invalidSet.end()
|| value_arr[arr[r]] > value_arr[arr[largest]])){
largest = r;
}
if (largest != ind){
swap(dict[arr[ind]], dict[arr[largest]]);
swap(arr[ind], arr[largest]);
max_heapify(largest, arr, value_arr);
}
}
void max_heap_insert(int ind, vector<int>& arr, const vector<int>& value_arr){
arr.push_back(ind);
dict[ind] = arr.size() - 1;
int i = arr.size() - 1;
while (i > 0 && (invalidSet.find(arr[PARENT(i)]) != invalidSet.end()
|| value_arr[arr[PARENT(i)]] < value_arr[arr[i]])){
swap(dict[arr[i]], dict[arr[PARENT(i)]]);
swap(arr[i], arr[PARENT(i)]);
i = PARENT(i);
}
}
void max_heap_delete(int ind, vector<int>& arr, const vector<int>& value_arr){
invalidSet.insert(ind);
max_heapify(dict[ind], arr, value_arr);
}
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> result;
if(nums.size() == 0)
return result;
vector<int> h;
h.reserve(nums.size());
for(int i = 0; i < k; i++)
max_heap_insert(i, h, nums);
result.push_back(nums[h[0]]);
for(int i = k; i < nums.size(); i++){
max_heap_delete(i - k, h, nums);
max_heap_insert(i, h, nums);
result.push_back(nums[h[0]]);
}
return result;
}
};
leetcode 239 Sliding Window Maximum的更多相关文章
- [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 ...
- [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 ...
- [leetcode] #239 Sliding Window Maximum (Hard)
原题链接 题意: 给定一个数组数字,有一个大小为k的滑动窗口,它从数组的最左边移动到最右边.你只能在窗口看到k个数字.每次滑动窗口向右移动一个位置. 记录每一次窗口内的最大值,返回记录的值. 思路: ...
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 【刷题-LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...
- 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...
- 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 ...
- LeetCode题解-----Sliding Window Maximum
题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...
- 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 ...
随机推荐
- 日期操作类--DateFormat类
简单的DateFormat格式化编码 时间模式字符串用来指定时间格式.在此模式中,所有的ASCII字母被保留为模式字母,定义如下: 字母 描述 示例 G 纪元标记 AD y 四位年份 2001 M 月 ...
- 【CITE】C#目录、文件、文件夹操作
1. 在一个目录下创建一个文件夹 if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path); ...
- nodeschool.io 5
~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...
- cf------(round)#1 C. Ancient Berland Circus(几何)
C. Ancient Berland Circus time limit per test 2 seconds memory limit per test 64 megabytes input sta ...
- SAP smartforms之Zebra print control language
因为在做个小标签的时候需要将部分字符旋转180度,在scn上找了很久也发布了自己的提问,不过最终的结果却不尽人意.Rotated text in smartforms need use the PCL ...
- AP聚类算法(Affinity propagation Clustering Algorithm )
AP聚类算法是基于数据点间的"信息传递"的一种聚类算法.与k-均值算法或k中心点算法不同,AP算法不需要在运行算法之前确定聚类的个数.AP算法寻找的"examplars& ...
- Check the difficulty of problems(POJ 2151)
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5457 ...
- Maven(1)-安装和配置
Maven(1)-安装和配置 一.本机必须安装好Jdk 二 .maven下载 http://maven.apache.org/download.cgi ,下载后把maven-bin解压到自己的目录即可 ...
- 六种流行的语言---C、C++、python、Java、php、C#比较[转]
语言大餐 回归正题,本文是六种语言连接mysql数据库的代码展示,在LZ尝试的过程中,无论是语言环境搭建.mysql依赖库的导入还是代码的风格,各种语言都各有千秋.接下来,我们就让这些语言一一登场吧. ...
- RedHat 安装MySQL数据库【转】
朋友购买了阿里云的服务器,服务器上自带有CentOS操作系统,但是开发软件需要自己安装,接下来将介绍本地RedHat Linux 5.10虚拟机上搭建Mysql数据库. 一.软件准备 (1)jdk ...