当前位置:网站首页>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();
}
}边栏推荐
- "APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare
- Day16 (regular expression, enumeration)
- Transformations of pytorch torch torch vision
- Add the author watermark plugin v1.4 update to the posts of elegant grass discuz plugin - some forums post errors and bugs have been fixed
- SAP ui5 Application Development Tutorial Part 30 - parameter transfer in the routing process of SAP ui5
- SAP ui5 date type sap ui. model. type. Analysis of date parsing format
- Aiot project that is an introduction to the basics of the Internet of things and can be implemented in practice
- Soft exam information system project manager_ Information system security management - Senior Information System Project Manager of soft test 026
- C language -- Sanzi chess
- 手机开户一般哪个证券公司好?手机开户是安全么?
猜你喜欢
How the sap ui5 framework performs single step debugging of batch requests

Array introduction plus example 01

No one reads the series. Source code analysis of copyonwritearraylist

Word quickly makes multiple single-sided table labels, number plates, etc

C language -- Sanzi chess

Volatile and JMM memory models
Do you know what a three-tier architecture is?

Instant messaging project (I)

Solve some prompt codes that pychar cannot recognize selenium

Double recursion in deep analysis merge sort
随机推荐
Technology inventory: past, present and future of Message Oriented Middleware
What changes have taken place in the project file after SAP ui5 tools ran the Fiori add deploy config command
Farewell to Lombok in 996
Semantic segmentation fcns in the wild: pixel level adaptive and constraint based adaptation
Go language map sorting (key/value sorting)
Leetcode topic [array] -36- effective Sudoku
MySQL tuning -- 02 -- slow query log
05 virtual machine stack
Guava-IO
Semantic segmentation cvpr2019-advance: advantageous enterprise minimization for domain adaptation in semantic segmentation
Oracle SQL statement operand: rounding, rounding, differentiation and formatting
[untitled]
Day13 (inner class, anonymous inner class, API common class)
How do product managers get started? How do they learn when no one takes them?
PIP connects to Tsinghua source by default
Code learning-cvpr2020 unsupervised domain adaptive semantic segmentation: intra advance
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
Wind farm visualization: wind farm data
Go quiz: considerations for function naming return value from the go interview question (more than 80% of people answered wrong)
Duplicate symbols for architecture i386 clang