当前位置:网站首页>Leetcode sword finger offer question brushing - day 27
Leetcode sword finger offer question brushing - day 27
2022-06-25 05:56:00 【DEGv587】
Leetcode The finger of the sword Offer How to brush questions :
The finger of the sword Offer 59 - I. Maximum sliding window
Solution 1 : Violent simulation
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length == 0) return new int[0];
int len = nums.length - k + 1;
List<Integer> list = new ArrayList<>();
int[] ret = new int[len];
for (int i = 0; i < len; ++i) {
int max = Integer.MIN_VALUE;
for (int j = i; j < i + k; ++j) {
max = Math.max(nums[j], max);
}
list.add(max);
}
for (int i = 0; i < len; ++i) {
ret[i] = list.get(i);
}
return ret;
}
}Solution 2 : A monotonous queue ( Optimized version )
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length == 0 || k == 0) return new int[0];
Deque<Integer> deque = new LinkedList<>();
int[] ret = new int[nums.length - k + 1];
int index = 0;
// No window interval is formed
for (int i = 0; i < k; ++i) {
// When the queue is not empty , Compare the current value with the value at the end of the queue , If it is greater than , Delete the value at the end of the queue
// The value in the queue is greater than the current value until the circular deletion , Or delete to a queue that is empty
while (!deque.isEmpty() && nums[i] > deque.peekLast()) {
deque.removeLast();
}
// After executing the above loop , The queue is either empty , Either the value is greater than the current value , Then add the current value to the queue
deque.addLast(nums[i]);
}
// Just after the window is formed , Add the first value of the queue to the queue
ret[index++] = deque.peekFirst();
// Window sections form
for (int i = k; i < nums.length; ++i) {
//i-k It's already outside the range , If the first is equal to nums[i-k], Then it means that the first value is no longer in the range , You need to remove
if (deque.peekFirst() == nums[i - k]) {
deque.removeFirst();
}
// Delete the value in the queue that is smaller than the current value
while (!deque.isEmpty() && deque.peekLast() < nums[i]) {
deque.removeLast();
}
// Add the current value to the queue
deque.addLast(nums[i]);
// Add the first value of the queue to arr Array
ret[index++] = deque.peekFirst();
}
return ret;
}
}The finger of the sword Offer 59 - II. The maximum value of the queue
solution : The sliding window ( deque )

class MaxQueue {
Deque<Integer> queue, deque;
public MaxQueue() {
// Initialize queue queue , The bidirectional queue deque
queue = new LinkedList<Integer>();
deque = new LinkedList<Integer>();
}
public int max_value() {
// When a two-way queue deque It's empty , Then return to -1 ;
// otherwise , return deque First element ;
return deque.isEmpty() ? -1 : deque.peekFirst();
}
public void push_back(int value) {
// Put the element value The team queue ;
queue.addLast(value);
// End the queue in a two-way queue all Less than value Element pop-up ( To maintain deque Nonmonotonic decreasing ), And put the elements value The team deque
while (!deque.isEmpty() && deque.peekLast() < value) {
deque.removeLast();
}
deque.addLast(value);
}
public int pop_front() {
// If queue queue It's empty , Then return directly -1 ;
if (queue.isEmpty()) return -1;
// otherwise , take queue The first element is out of the team ;
// if deque First element and queue First element equal , Will deque The first element is out of the team ( To keep two lines The elements are the same ) ;
if (queue.peekFirst().equals(deque.peekFirst())) {
deque.removeFirst();
}
return queue.removeFirst();
}
}边栏推荐
- MySQL tuning -- 02 -- slow query log
- Synchonized introduction
- Leetcode topic [array] -36- effective Sudoku
- MySQL uses the where condition to find strange results: solve
- Click to send text messages without response is a common problem for many users in building the elegant grass Dragonfly Q system - solve the problem of clicking to send text messages without response
- Design of IM login server and message server
- Is the securities account of Qiantang education safe? Is it reliable?
- Deep analysis of epoll reactor code
- PIP connects to Tsinghua source by default
- Ethernet
猜你喜欢
SAP Fiori tools and corresponding cli (command line interface)

Code learning-cvpr2020 unsupervised domain adaptive semantic segmentation: intra advance
SAP ui5 application development tutorial 32 - how to create a custom SAP ui5 control

Day22 send request and parameterization using JMeter
![[day40 literature extensive reading] space and time in the child's mind: metallic or atomic](/img/98/10b3e63c9609990c51b619d9ca6179.jpg)
[day40 literature extensive reading] space and time in the child's mind: metallic or atomic

"APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare

Click to send text messages without response is a common problem for many users in building the elegant grass Dragonfly Q system - solve the problem of clicking to send text messages without response

Interface learning

MySQL uses the where condition to find strange results: solve

ThreadLocal
随机推荐
Use of MySQL variables
Farewell to Lombok in 996
What is hybrid web containers for SAP ui5
JMeter stress testing and agent recording
D compile time reflection
Invalid bound statement (not found)
Deep analysis of recursion in quick sorting
Yunda's cloud based business in Taiwan construction 𞓜 practical school
First blog
Some common errors and solutions of using SAP ui5 to consume OData services
Which securities company is good for opening a mobile account? Is it safe to open a mobile account?
Day18 (set, generic, hash table, tree, stack and queue, graph, array and linked list)
SAP ui5 application development tutorial 32 - how to create a custom SAP ui5 control
Pat 1045 quick sort
C language -- Sanzi chess
Monkey test of APP automation
C simple operation mongodb
Linus' speech recordings, which were lost in 1994, were made public
MySQL uses the where condition to find strange results: solve
A method of automatic continuation of previous tables in word table