sliding window
2015-06-05 06:14
429 查看
A long array A[] is given to you. There is a sliding window of size w which
is moving from the very left of the array to the very right. You can only see the w numbers
in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and w is
3.
Input:
A long array A[], and a window width w
Output:
An array B[], B[i] is the maximum value of from A[i] to A[i+w-1]
Requirement:
Find a good optimal way to get B[i]
The double-ended queue is the perfect data structure for this problem. It supports insertion/deletion from the front and back. The trick is to find a way such that the largest element in the window would always appear in the front of the queue. How would you
maintain this requirement as you push and pop elements in and out of the queue?
Besides, you might notice that there are some redundant elements in the queue that we shouldn’t even consider about. For example, if the current queue has the elements: [10 5 3], and a new element in the window has the element 11. Now, we could have emptied
the queue without considering elements 10, 5, and 3, and insert only element 11 into the queue.
A natural way most people would think is to try to maintain the queue size the same as the window’s size. Try to break away from this thought and try to think outside of the box. Removing redundant elements and storing only elements that need to be considered
in the queue is the key to achieve the efficient O(n) solution below.
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
public class Solution {
public static int[] slidingWindowMax(int[] nums, int k)
{
int n = nums.length - k + 1;
Deque<Integer> queue = new ArrayDeque<Integer>();
int[] max = new int
;
for (int i = 0; i < nums.length; i++) {
while(!queue.isEmpty() && nums[queue.getLast()] <= nums[i]) {
queue.removeLast();
}
queue.addLast(i);
if (i < k - 1) continue;
while(!queue.isEmpty() && i - k >= queue.getFirst())
queue.removeFirst();
max[i - k + 1] = nums[queue.getFirst()];
}
return max;
}
public static void main(String[] args) {
int[] nums = {1,3,-1,-3,5,3,6};
int[] ret = slidingWindowMax(nums, 3);
for (int i : ret) {
System.out.println(i);
}
}
}
is moving from the very left of the array to the very right. You can only see the w numbers
in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and w is
3.
Window position Max --------------- ----- [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
Input:
A long array A[], and a window width w
Output:
An array B[], B[i] is the maximum value of from A[i] to A[i+w-1]
Requirement:
Find a good optimal way to get B[i]
The double-ended queue is the perfect data structure for this problem. It supports insertion/deletion from the front and back. The trick is to find a way such that the largest element in the window would always appear in the front of the queue. How would you
maintain this requirement as you push and pop elements in and out of the queue?
Besides, you might notice that there are some redundant elements in the queue that we shouldn’t even consider about. For example, if the current queue has the elements: [10 5 3], and a new element in the window has the element 11. Now, we could have emptied
the queue without considering elements 10, 5, and 3, and insert only element 11 into the queue.
A natural way most people would think is to try to maintain the queue size the same as the window’s size. Try to break away from this thought and try to think outside of the box. Removing redundant elements and storing only elements that need to be considered
in the queue is the key to achieve the efficient O(n) solution below.
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
public class Solution {
public static int[] slidingWindowMax(int[] nums, int k)
{
int n = nums.length - k + 1;
Deque<Integer> queue = new ArrayDeque<Integer>();
int[] max = new int
;
for (int i = 0; i < nums.length; i++) {
while(!queue.isEmpty() && nums[queue.getLast()] <= nums[i]) {
queue.removeLast();
}
queue.addLast(i);
if (i < k - 1) continue;
while(!queue.isEmpty() && i - k >= queue.getFirst())
queue.removeFirst();
max[i - k + 1] = nums[queue.getFirst()];
}
return max;
}
public static void main(String[] args) {
int[] nums = {1,3,-1,-3,5,3,6};
int[] ret = slidingWindowMax(nums, 3);
for (int i : ret) {
System.out.println(i);
}
}
}
相关文章推荐
- Haar分类器(人脸识别、人眼识别)
- Array: 常用Java操作
- Node.js 学习资源总结
- Simplify Path
- 我的调试输出 _TRACE 第三版
- HashTable HashMap HashSet区别(java) [From 爱做饭的小莹子]
- Global Linear Model
- 我的System Design总结 [From Mitbbs]
- LeetCode解题报告--String to Integer (atoi)
- 委内瑞拉支付市场概况
- sgu252:Railway Communication(费用流)
- ASP.NET MVC和WebForm 轻松实现前端和后端的双重验证 jquery.validate+ValidationSugar
- Foundation框架发送消息
- Shell 基础
- ubuntu 取消打印队列命令
- [hdu4599]期望DP
- Climbing Stairs
- Maximal Square
- HackerRank - "Stars"
- Codeforces Round #306 (Div. 2)