当前位置:网站首页>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(); */
边栏推荐
- Leetcode14 longest public prefix
- (C语言)八进制转换十进制
- Differences between nodes and sharding in ES cluster
- post请求体内容无法重复获取
- Deep understanding of P-R curve, ROC and AUC
- mysql表的增删改查(进阶)
- OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
- 史上最易懂的f-string教程,收藏这一篇就够了
- Le tutoriel F - String le plus facile à comprendre de l'histoire.
- MySQL and PostgreSQL methods to grab slow SQL
猜你喜欢

mysql数据库基础

Drools dynamically add, modify, and delete rules

Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately

Sort---

CDA数据分析——AARRR增长模型的介绍、使用
![[C language] convert decimal numbers to binary numbers](/img/9b/1848b68b95d98389ed985c83f2e856.png)
[C language] convert decimal numbers to binary numbers

深入理解P-R曲线、ROC与AUC

arcgis js 4. Add pictures to x map

Brush questions --- binary tree --2

kubenetes中port、targetPort、nodePort、containerPort的区别与联系
随机推荐
Performance tuning project case
倍增 LCA(最近公共祖先)
Codeforces 771 div2 B (no one FST, refers to himself)
Jenkins用户权限管理
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
基于Arduino和ESP8266的连接手机热点实验(成功)
【C语言】杨辉三角,自定义三角的行数
drools执行指定的规则
记录一下MySql update会锁定哪些范围的数据
Natural language processing series (II) -- building character level language model using RNN
Leetcode739 每日温度
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
Lombok common annotations
Fastdateformat why thread safe
LeetCode—剑指 Offer 37、38
Find the common ancestor of any two numbers in a binary tree
Tas (file d'attente prioritaire)
CPU指令集介绍
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
Deep understanding of P-R curve, ROC and AUC