leetcode面试准备:Sliding Window Maximum
2015-09-11 17:16
399 查看
leetcode面试准备:Sliding Window Maximum
1 题目
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 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
Therefore, return the max sliding window as [3,3,5,5,6,7].
Note:
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.
Hint:
How about using a data structure such as deque (double-ended queue)?
The queue size need not be the same as the window’s size.
Remove redundant elements and the queue should store only elements that need to be considered.
接口:
public int[] maxSlidingWindow(int[] nums, int k)
2 思路
Brute force solution is O(nw)Use heap, when window moves, delete the first one in the window, add the next one into the window. The run time complexity is O(n lg w).
Use double-ended queue.
The obvious solution with run time complexity of O(nw) is definitely not efficient enough. Every time the window is moved, we have to search for the maximum from w elements in the window.
We need a data structure where we can store the candidates for maximum value in the window and discard the element, which are outside the boundary of window. For this, we need a data structure in which we can edit at both the ends, front and back. Deque is a perfect candidate for this problem.
We are trying to find a way in which, we need not search for maximum element among all in the window. We will make sure that the largest element in the window would always appear in the front of the queue.
While traversing the array in forward direction if we find a window where element A[i] > A[j] and i > j, we can surely say that A[j], will not be the maximum element for this and succeeding windows. So there is no need of storing j in the queue and we can discard A[j] forever.
For example, if the current queue has the elements: [4 13 9], and a new element in the window has the element 15. Now, we can empty the queue without considering elements 4, 13, and 9, and insert only element 15 into the queue.
3 代码
// 暴力解法:采用左右边界滑动窗口 // Time:O(n*k) public int[] maxSlidingWindow1(int[] nums, int k) { int len = nums.length; if (len == 0) return new int[0]; int left = 0, right = k - 1; int[] res = new int[len - k + 1]; while (right < len) { res[left] = nums[left]; for (int i = left + 1; i <= right; i++) { res[left] = Math.max(res[left], nums[i]); } left++; right++; } return res; } // 用TreeSet实现堆,减少查询最大值的效率,降低了复杂度。 // Wrong Answer:无法解决重复元素的问题。 // Time:O(nlogK) Space:O(k) public int[] maxSlidingWindow2(int[] nums, int k) { int len = nums.length; if (len == 0) return new int[0]; int[] res = new int[len - k + 1]; TreeSet<Integer> heap = new TreeSet<Integer>(); for (int i = 0; i < k - 1; i++) { heap.add(nums[i]); } for (int i = k - 1; i < len; i++) { heap.add(nums[i]); int index = i - k + 1; res[index] = heap.last(); heap.remove(nums[index]); } return res; } // 用双端队列来解决,时间复杂度降到了O(n) // Time:O(n) Space:O(k) public int[] maxSlidingWindow(int[] nums, int k) { int len = nums.length; if (len == 0) return new int[0]; int[] res = new int[len - k + 1]; Deque<Integer> dq = new LinkedList<Integer>(); for (int i = 0; i < len; i++) { int data = nums[i]; while (!dq.isEmpty() && dq.getLast() < data) { dq.removeLast(); } dq.add(data); if (i < k - 1) { continue; } int index = i - k + 1; res[index] = dq.getFirst(); if (res[index] == nums[index]) { dq.removeFirst(); } } return res; }
4 总结
三种方法解决,如何解决堆储存重复元素的问题。相关文章推荐
- 黑马程序员_java11_多态
- 程序员所应具备的品质
- 面试题——分析从输入url到页面返回的过程(或者查询返回过程)
- 《程序员面试金典》--基本字符串压缩
- 百度面试题--盒子凸起一个角效果实现的讨论
- 不想写歌的码农,不是个好电影人
- 不想写歌的码农,不是个好电影人
- 黑马程序员-----Java基础-----File,Properties和其他IO处理流
- leetcode面试准备:Simplify Path
- 每天工作4小时的程序员
- 历史上最伟大的12位程序员
- leetcode面试准备:Kth Largest Element in an Array
- 薪资谈判应该避免的7个错误-兄弟连IT教育
- 多线程面试题(迅雷)
- 多线程面试题(Google)
- git 在开发中程序员最基本最有用的几步
- 程序员技术
- 黑马程序员--反射
- 【转】java架构师之路:JAVA程序员必看的15本书的电子版下载地址
- 最老程序员开发实训9--Android---应用介绍页面实现1