当前位置:网站首页>239. Sliding window maximum
239. Sliding window maximum
2022-07-03 12:11:00 【zwanying】
- Maximum sliding window
Force link 239. Maximum sliding window
Give you an array of integers nums, There is a size of k The sliding window of the array moves from the leftmost side to the rightmost side of the array . You can only see... In the sliding window k A digital . The sliding window moves only one bit to the right at a time .
Returns the maximum value in the sliding window .
Example 1:
Input :nums = [1,3,-1,-3,5,3,6,7], k = 3
Output :[3,3,5,5,6,7]
explain :
Position of sliding window Maximum
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Their thinking :
Violence law can solve .
Here, linear time complexity can be achieved by using double ended queues .
1、 Use queues to store larger elements . Team leader is the biggest element .
2、 If the element to be added is larger than the end of the queue , Take out the elements at the end of the team , Ensure that the queue is arranged in an increasing order .( You only need to save larger values in the queue , Small values do not need to be saved )
3、 If the element to be added is smaller than the end of the queue , Add directly to the end of the team .
4、 If the sliding window moves , The removed element is exactly equal to the team leader element , Remove the team leader element .
deque
deque Deque extends Queue, Elements can be inserted at both ends to get
offerFirst
offerLast
pollFirst
pollLast
peekFirst
peekLast
Realization
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
// Keep larger elements in the queue , If there is a bigger , Cover
// If you remove the team leader , Delete .
int[] result = new int[nums.length-k+1];
int x = 0;
if(nums==null || nums.length<=0){
return result;
}
Deque<Integer> deque = new LinkedList<>();
for(int i=0;i<nums.length;i++){
if(!deque.isEmpty() && deque.peekLast()>nums[i]){
deque.offerLast(nums[i]);
}else{
// Be careful : Ensure the order of the queue , When inserting elements , If the tail element is smaller than the newly inserted element , Delete the tail element , We need to continue to compare new tail elements .
while(!deque.isEmpty() && deque.peekLast()<nums[i]){
deque.pollLast();
}
deque.offerLast(nums[i]);
}
if(i>=k-1){
result[x++]=deque.peekFirst();
if(deque.peekFirst() == nums[i-k+1]){
deque.pollFirst();
}
}
}
return result;
}
}
边栏推荐
- Groovy测试类 和 Junit测试
- Flutter: self study system
- DEJA_VU3D - Cesium功能集 之 053-地下模式效果
- MySQL uses the method of updating linked tables with update
- Jsup crawls Baidu Encyclopedia
- OpenGL 绘制彩色的三角形
- Visual studio 2022 downloading and configuring opencv4.5.5
- Solve msvcp120d DLL and msvcr120d DLL missing
- vulnhub之tomato(西红柿)
- Socket TCP for network communication (I)
猜你喜欢

New features of ES6

During FTP login, the error "530 login incorrect.login failed" is reported

vulnhub之pyexp

Colleagues wrote a responsibility chain model, with countless bugs

Vulnhub's Tomato (tomato)

Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements

Kubernetes three dozen probes and probe mode

ES6新特性

Vulnhub geminiinc

QT OpenGL texture map
随机推荐
regular expression
OpenGL 着色器使用
PHP get the file list and folder list under the folder
Use of QT OpenGL camera
PHP導出word方法(一mht)
previous permutation lintcode51
Concurrent programming - singleton
OpenGL draws colored triangles
Quantitative calculation research
Symlink(): solution to protocol error in PHP artisan storage:link on win10
laravel 时区问题timezone
vulnhub之presidential
OpenGL index cache object EBO and lineweight mode
New features of ES6
023(【模板】最小生成树)(最小生成树)
Shutter: about inheritedwidget
Is BigDecimal safe to calculate the amount? Look at these five pits~~
Redis
During FTP login, the error "530 login incorrect.login failed" is reported
typeScript