当前位置:网站首页>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(); */
边栏推荐
- Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
- H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
- CDA数据分析——Excel数据处理的常见知识点归纳
- SparkContext: Error initializing SparkContext解决方法
- Read the Flink source code and join Alibaba cloud Flink group..
- Filtre de profondeur de la série svo2
- MySQL与PostgreSQL抓取慢sql的方法
- (C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- (C语言)八进制转换十进制
猜你喜欢

How to Create a Beautiful Plots in R with Summary Statistics Labels

CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节

HR wonderful dividing line

5g era, learning audio and video development, a super hot audio and video advanced development and learning classic

Pytorch builds LSTM to realize clothing classification (fashionmnist)

Larvel modify table fields

ThreadLocal的简单理解
![[geek challenge 2019] upload](/img/04/731323142161a4994c14fedae38b81.jpg)
[geek challenge 2019] upload

Natural language processing series (II) -- building character level language model using RNN

Heap (priority queue)
随机推荐
SVO2系列之深度滤波DepthFilter
CDA data analysis -- Introduction and use of aarrr growth model
二分刷题记录(洛谷题单)区间的甄别
Mish-撼动深度学习ReLU激活函数的新继任者
drools执行完某个规则后终止别的规则执行
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
Dynamic debugging of multi file program x32dbg
Orb-slam2 data sharing and transmission between different threads
CONDA common command summary
conda常用命令汇总
Codeforces 771 div2 B (no one FST, refers to himself)
Post request body content cannot be retrieved repeatedly
Brush questions --- binary tree --2
深入理解PyTorch中的nn.Embedding
(C language) octal conversion decimal
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
Yygh-9-make an appointment to place an order
How to Visualize Missing Data in R using a Heatmap
JZ63 股票的最大利润