当前位置:网站首页>Leetcode - Sword finger offer 59 - I, 59 - II
Leetcode - Sword finger offer 59 - I, 59 - II
2022-07-02 12:21:00 【Ostrich5yw】
The finger of the sword Offer 59 - I. Maximum sliding window 、59 - II. The maximum value of the queue
Title Description :[59 - I]
Given an array nums And the size of the sliding window k, Please find the maximum value in all sliding windows .[59 - II]
Please define a queue and implement the function max_value Get the maximum in the queue , Function required max_value、push_back and pop_front The average time complexity of is O(1).
If the queue is empty ,pop_front and max_value Need to return -1
Key points of investigation :
The first 59 - I topic Use priority queues ( Pile up ) Realize the sorting of elements in the window , Loop through the simulation window sliding
The first 59 - II topic Use a monotonic queue for assistance , Save the size relationship of queue elements .
The first 59 - I topic
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;
}
The first 59 - II topic
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 It's a monotonous queue , When each element enters the team , Will kick out the inner elements smaller than it in front , In order to ensure that the queue is always monotonically decreasing
help.removeLast();
}
help.addLast(value);
}
public int pop_front() {
if(que.size() == 0)
return -1;
int val = que.removeFirst();
if(help.getFirst() == val) {
// First in, first out , So we just need to compare the out of team elements help Team leader element is enough
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(); */
边栏推荐
- Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
- Deep understanding of NN in pytorch Embedding
- Codeforces 771 div2 B (no one FST, refers to himself)
- CDA data analysis -- Introduction and use of aarrr growth model
- 子线程获取Request
- Simple understanding of ThreadLocal
- Go学习笔记—基于Go的进程间通信
- On data preprocessing in sklearn
- ThreadLocal的简单理解
- CDH6之Sqoop添加数据库驱动
猜你喜欢
随机推荐
二分刷题记录(洛谷题单)区间的甄别
CDA数据分析——AARRR增长模型的介绍、使用
(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?
LeetCode—<动态规划专项>剑指 Offer 19、49、60
寻找二叉树中任意两个数的公共祖先
drools执行String规则或执行某个规则文件
Use sqoop to export ads layer data to MySQL
史上最易懂的f-string教程,收藏這一篇就够了
Go学习笔记—多线程
Those logs in MySQL
Jenkins user rights management
Maximum profit of jz63 shares
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
Differences between nodes and sharding in ES cluster
[C language] Yang Hui triangle, customize the number of lines of the triangle
Intel 内部指令 --- AVX和AVX2学习笔记
Day12 control flow if switch while do While guessing numbers game
On data preprocessing in sklearn
Leetcode14 longest public prefix
MySQL indexes and transactions









