当前位置:网站首页>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(); */
边栏推荐
- Those logs in MySQL
- [C language] convert decimal numbers to binary numbers
- The most understandable f-string tutorial in history, collecting this one is enough
- Differences between nodes and sharding in ES cluster
- 浅谈sklearn中的数据预处理
- Sub thread get request
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- CDA data analysis -- Introduction and use of aarrr growth model
- Performance tuning project case
- 求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
猜你喜欢
arcgis js 4.x 地图中加入图片
Performance tuning project case
SparkContext: Error initializing SparkContext解决方法
ThreadLocal的简单理解
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
ES集群中节点与分片的区别
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Deep understanding of P-R curve, ROC and AUC
Heap (priority queue)
(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?
随机推荐
Leetcode739 daily temperature
计算二叉树的最大路径和
Simple use of drools decision table
LeetCode—剑指 Offer 37、38
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间
Drools executes the specified rule
Natural language processing series (II) -- building character level language model using RNN
Uniapp uni list item @click, uniapp uni list item jump with parameters
Use sqoop to export ads layer data to MySQL
Depth filter of SvO2 series
(C language) octal conversion decimal
字符串回文hash 模板题 O(1)判字符串是否回文
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
Go learning notes - go based interprocess communication
Applet link generation
Le tutoriel F - String le plus facile à comprendre de l'histoire.
Adding database driver to sqoop of cdh6
drools执行String规则或执行某个规则文件
[C language] convert decimal numbers to binary numbers