当前位置:网站首页>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();
}
}
边栏推荐
- DOM proficient? What is the difference between node and elment?
- Summary of 6 common methods of visual deep learning model architecture
- Voxel based and second network learning
- Depth of binary tree
- Transformations of pytorch torch torch vision
- Mongodb basic concept learning - Documentation
- Part 34 of SAP ui5 application development tutorial - device adaptation of SAP ui5 application based on device type
- Farewell to Lombok in 996
- Various errors and solutions encountered when deploying SAP ui5 application to ABAP server with SAP Fiori tools
- Volatile and JMM memory models
猜你喜欢
Use generator-easy-ui5 to quickly create the engineering structure of SAP ui5 applications
SAP ui5 beginner tutorial No. 27 - unit test tool quNit introduction trial version for SAP ui5 application
Use of MySQL variables
Solve some prompt codes that pychar cannot recognize selenium
Learn the interface test, see it is very good, and make a note
Soft exam information system project manager_ Information system security management - Senior Information System Project Manager of soft test 026
Mongodb basic concept learning - set
ERDAS 9.2 installation tutorial
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?
Unsupervised domain adaptation in semantic segmentation:a review unsupervised domain adaptation in semantic segmentation: a review
随机推荐
手机开户一般哪个证券公司好?手机开户是安全么?
Use of collection
[untitled]
CSDN cerebral palsy bug has wasted nearly two hours of hard work
CST8227
JS to realize the encapsulation of the function of obtaining the mouse click position
Multithreading and thread pool
05 virtual machine stack
MySQL tuning --01--- optimization steps and system performance parameters
ERDAS 9.2 installation tutorial
"APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare
C simple operation mongodb
Understanding the dynamic mode of mongodb document
I got to know data types and function variables for the first time. I learned data types and function variables together today and yesterday, so I saved them in the first issue to record.
16 application problem solving
Create a complete binary tree in array order
Try with resource close resource flow
Introduction to MySQL test run test framework
JS implementation mouse can achieve the effect of left and right scrolling
C switch nested syntax