当前位置:网站首页>LeetCode—剑指 Offer 59 - I、59 - II
LeetCode—剑指 Offer 59 - I、59 - II
2022-07-02 09:43:00 【Ostrich5yw】
剑指 Offer 59 - I. 滑动窗口的最大值、59 - II. 队列的最大值
题目描述:[59 - I]
给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。[59 - II]
请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的均摊时间复杂度都是O(1)。
若队列为空,pop_front 和 max_value 需要返回 -1
考察重点:
第59 - I题使用优先队列(堆)实现窗口内元素排序,循环遍历模拟窗口滑动
第59 - II题使用一个单调队列进行辅助,保存队列元素大小关系。
第59 - I题
class Num{
int pos;
int val;
public Num(int pos, int val){
this.pos = pos;
this.val = val;
}
}
public int[] maxSlidingWindow(int[] nums, int k) {
int[] res = new int[nums.length - k + 1];
int recPos = 0;
int left = 0, right = 0;
int nLen = nums.length;
if(nLen == 0){
return new int[]{
};
}
PriorityQueue<Num> pq = new PriorityQueue<>(new Comparator<Num>() {
@Override
public int compare(Num o1, Num o2) {
if(o1.val > o2.val){
return -1;
}else{
return 1;
}
}
});
while(right < nLen){
Num num = new Num(right, nums[right]);
pq.add(num);
if(right - left >= k){
for(Num p : pq){
if(p.pos == left){
pq.remove(p);
left ++;
break;
}
}
}
if(right - left == k - 1)
res[recPos++] = pq.peek().val;
right ++;
}
return res;
}
第59 - II题
class MaxQueue {
public Deque<Integer> que;
public Deque<Integer> help;
public MaxQueue() {
que = new ArrayDeque<>();
help = new ArrayDeque<>();
}
public int max_value() {
if(que.size() == 0)
return -1;
int val = help.getFirst();
return val;
}
public void push_back(int value) {
que.addLast(value);
while(help.size() != 0 && value > help.getLast()){
// help是一个单调队列,每个元素入队时,都会踢出前面比它小的对内元素,以此保障队列始终单调递减
help.removeLast();
}
help.addLast(value);
}
public int pop_front() {
if(que.size() == 0)
return -1;
int val = que.removeFirst();
if(help.getFirst() == val) {
// 先进一定先出,所以只需要比较出队元素是不是help队头元素即可
help.removeFirst();
}
return val;
}
}
/** * Your MaxQueue object will be instantiated and called as such: * MaxQueue obj = new MaxQueue(); * int param_1 = obj.max_value(); * obj.push_back(value); * int param_3 = obj.pop_front(); */
边栏推荐
- Orb-slam2 data sharing and transmission between different threads
- 深入理解PyTorch中的nn.Embedding
- 基于Arduino和ESP8266的连接手机热点实验(成功)
- Initial JDBC programming
- mysql表的增删改查(进阶)
- CDA数据分析——Excel数据处理的常见知识点归纳
- 【C语言】十进制数转换成二进制数
- 输入一个三位的数字,输出它的个位数,十位数、百位数。
- From scratch, develop a web office suite (3): mouse events
- (C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
猜你喜欢
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
Filtre de profondeur de la série svo2
MySQL与PostgreSQL抓取慢sql的方法
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
WSL 2 will not be installed yet? It's enough to read this article
How to Easily Create Barplots with Error Bars in R
Power Spectral Density Estimates Using FFT---MATLAB
Tas (file d'attente prioritaire)
PyTorch搭建LSTM实现服装分类(FashionMNIST)
mysql索引和事务
随机推荐
【C语言】十进制数转换成二进制数
Lombok common annotations
Post request body content cannot be retrieved repeatedly
Leetcode122 买卖股票的最佳时机 II
Maximum profit of jz63 shares
Log4j2
ThreadLocal的简单理解
子线程获取Request
drools动态增加、修改、删除规则
史上最易懂的f-string教程,收藏這一篇就够了
conda常用命令汇总
drools中then部分的写法
Repeat, tile and repeat in pytorch_ The difference between interleave
自然语言处理系列(二)——使用RNN搭建字符级语言模型
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
Mish shake the new successor of the deep learning relu activation function
Leetcode122 the best time to buy and sell stocks II
post请求体内容无法重复获取
FastDateFormat为什么线程安全
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?