当前位置:网站首页>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(); */
边栏推荐
- 寻找二叉树中任意两个数的公共祖先
- 测试左移和右移
- kubenetes中port、targetPort、nodePort、containerPort的区别与联系
- Codeforces 771-div2 C (trouble, permutation is not very good)
- The most understandable f-string tutorial in history, collecting this one is enough
- The differences and relationships among port, targetport, nodeport and containerport in kubenetes
- The blink code based on Arduino and esp8266 runs successfully (including error analysis)
- Addition, deletion, modification and query of MySQL table (Advanced)
- Deep understanding of NN in pytorch Embedding
- (C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
猜你喜欢
China traffic sign detection data set
使用Sqoop把ADS层数据导出到MySQL
CONDA common command summary
ES集群中节点与分片的区别
Embedded Software Engineer career planning
Heap (priority queue)
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
刷题---二叉树--2
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
寻找二叉树中任意两个数的公共祖先
随机推荐
Adding database driver to sqoop of cdh6
Go学习笔记—多线程
mysql表的增删改查(进阶)
SSH automatically disconnects (pretends to be dead) after a period of no operation
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
Leetcode topic [array] -540- single element in an ordered array
Codeforces 771 div2 B (no one FST, refers to himself)
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
HR wonderful dividing line
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
高性能纠删码编码
Le tutoriel F - String le plus facile à comprendre de l'histoire.
还不会安装WSL 2?看这一篇文章就够了
子线程获取Request
China traffic sign detection data set
drools执行指定的规则
Map和Set
计算二叉树的最大路径和
Go learning notes - multithreading
From scratch, develop a web office suite (3): mouse events