★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qwalqmji-mc.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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. Return the max sliding window.git
Example:github
Input: nums = , and k = 3 Output: 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 [1,3,-1,-3,5,3,6,7][3,3,5,5,6,7] Explanation:
Note:
You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty array.數組
Follow up:
Could you solve it in linear time?微信
給定一個數組 nums,有一個大小爲 k 的滑動窗口從數組的最左側移動到數組的最右側。你只能夠看到在滑動窗口 k 內的數字。滑動窗口每次只向右移動一位。app
返回滑動窗口最大值。spa
示例:code
輸入: nums = , 和 k = 3 輸出: 滑動窗口的位置 最大值 --------------- ----- [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[1,3,-1,-3,5,3,6,7][3,3,5,5,6,7] 解釋:
注意:htm
你能夠假設 k 老是有效的,1 ≤ k ≤ 輸入數組的大小,且輸入數組不爲空。blog
進階:
你能在線性時間複雜度內解決此題嗎?
168ms
1 class Solution { 2 func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { 3 guard k > 0 && nums.count >= k else { 4 return [] 5 } 6 7 if k == 1 { 8 return nums 9 } 10 var res = [Int]() 11 var max_tuple = (nums[0],0) 12 13 for i in 0..<k { 14 if nums[i] > max_tuple.0 { 15 max_tuple = (nums[i],i) 16 } 17 } 18 res.append(max_tuple.0) 19 for i in k..<nums.count { 20 21 if nums[i] > max_tuple.0 { 22 max_tuple = (nums[i],i) 23 } else if max_tuple.1 == (i-k){ 24 var pos = max_tuple.1 25 max_tuple = (nums[i],i) 26 for j in (pos + 1)...(i-1) { 27 if nums[j] > max_tuple.0 { 28 max_tuple = (nums[j],j) 29 } 30 } 31 } 32 res.append(max_tuple.0) 33 } 34 35 return res 36 } 37 }
184ms
1 class Solution { 2 func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { 3 guard nums.count > 0 && k > 0 else { return [] } 4 var i = 0 5 var maxWindow = [Int]() 6 while k + i <= nums.count { 7 let slice = nums[i..<i+k] 8 if maxWindow.count == 0 { 9 maxWindow.append(slice.max()!) 10 } else { 11 let lastMax = maxWindow.last! 12 if lastMax == nums[i - 1] { 13 maxWindow.append(slice.max()!) 14 } else { 15 maxWindow.append(max(lastMax, slice.last!)) 16 } 17 } 18 19 i += 1 20 } 21 return maxWindow 22 } 23 }
188ms
1 class Solution { 2 func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { 3 if k > nums.count { 4 return [] 5 } 6 7 var result = [Int]() 8 9 var biDirectionalQueue = [Int]() 10 11 for i in 0..<nums.count { 12 13 while(!biDirectionalQueue.isEmpty && biDirectionalQueue[0]+k <= i) { 14 biDirectionalQueue.remove(at: 0) 15 } 16 17 while(!biDirectionalQueue.isEmpty && nums[biDirectionalQueue[biDirectionalQueue.count - 1]] < nums[i]) { 18 biDirectionalQueue.removeLast() 19 } 20 21 biDirectionalQueue.append(i) 22 23 if i >= k-1 { 24 result.append(nums[biDirectionalQueue[0]]) 25 } 26 } 27 28 return result 29 } 30 }
204ms
1 class Solution { 2 func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { 3 if nums.isEmpty || k <= 0 { 4 return [] 5 } 6 var window: [Int] = [], res: [Int] = [] 7 for i in 0..<nums.count { 8 let x = nums[i] 9 if i >= k && window[0] <= i - k { 10 window.removeFirst() 11 } 12 while !window.isEmpty && nums[window.last!] <= x { 13 window.popLast() 14 } 15 window.append(i) 16 if i >= k - 1 { 17 res.append(nums[window.first!]) 18 } 19 } 20 return res 21 } 22 }
208ms
1 class Solution { 2 func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { 3 guard nums.count > 0 else { return [] } 4 5 var output: [Int] = [] 6 var deQueue: [Int] = [] 7 let n = nums.count 8 for index in 0..<k { 9 while deQueue.count > 0 && nums[index] >= nums[deQueue.last!] { 10 deQueue.removeLast() 11 } 12 deQueue.append(index) 13 } 14 for index in k..<n { 15 output.append(nums[deQueue.first!]) 16 17 while deQueue.count > 0 && deQueue.first! <= index - k { 18 deQueue.removeFirst() 19 } 20 21 while deQueue.count > 0 && nums[index] >= nums[deQueue.last!] { 22 deQueue.removeLast() 23 } 24 deQueue.append(index) 25 } 26 27 output.append(nums[deQueue.first!]) 28 return output 29 } 30 }
212ms
1 class Solution { 2 func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { 3 var answer = [Int]() 4 var q = [Int]() 5 6 for i in 0..<nums.count { 7 while !q.isEmpty && q.first! < i - k + 1 { 8 q.removeFirst() 9 } 10 11 while !q.isEmpty && nums[q.last!] < nums[i] { 12 q.removeLast() 13 } 14 15 q.append(i) 16 17 if i >= k - 1 { 18 answer.append(nums[q.first!]) 19 } 20 } 21 22 return answer 23 } 24 }
224ms
1 class MonQueue { 2 private var queue = [Int]() 3 4 func push(_ el: Int) { 5 while !queue.isEmpty && queue.last! < el { 6 queue.removeLast() 7 } 8 9 queue.append(el) 10 } 11 12 func pop(_ el: Int) { 13 if !queue.isEmpty && queue.first! == el { 14 queue.removeFirst() 15 } 16 } 17 18 func front() -> Int? { 19 return queue.first 20 } 21 } 22 class Solution { 23 func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { 24 var res = [Int]() 25 26 guard nums.count > 0 else { 27 return res 28 } 29 30 var monQueue = MonQueue() 31 32 for i in 0..<k { 33 monQueue.push(nums[i]) 34 } 35 36 res.append(monQueue.front()!) 37 38 for i in k..<nums.count { 39 let elToRemove = nums[i-k] 40 let elToAdd = nums[i] 41 monQueue.push(elToAdd) 42 monQueue.pop(elToRemove) 43 res.append(monQueue.front()!) 44 } 45 46 return res 47 } 48 }
256ms
1 class Solution { 2 func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { 3 4 var res = [Int]() 5 var q = [Int]() 6 for (i, num) in nums.enumerated() { 7 if !q.isEmpty, q.first! == i-k { q.removeFirst() } 8 while !q.isEmpty, nums[q.last!] < num { q.removeLast() } 9 q.append(i) 10 if i >= k-1 { res.append(nums[q.first!])} 11 } 12 return res 13 } 14 }