Created
September 28, 2020 09:18
-
-
Save charlieInDen/4efbbd277f317dca18c8fea2f894b9a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
var deque: [Int] = [] | |
var nums: [Int]! = [] | |
func cleanDeque(_ i: Int, _ k: Int) { | |
if deque.isEmpty == false{ | |
if let first = deque.first, first == i - k { | |
deque.removeFirst() | |
} | |
} | |
while deque.isEmpty == false { | |
if let last = deque.last, nums[i] > nums[last] { | |
deque.removeLast() | |
}else { | |
break | |
} | |
} | |
} | |
func maxSlidingWindow(_ numsa: [Int], _ k: Int) -> [Int] { | |
nums = numsa | |
let len = nums.count | |
if len * k == 0 { return [] } | |
if k == 1 { return nums } | |
var maxId = 0 | |
for i in 0..<k { | |
cleanDeque(i, k) | |
deque.append(i) | |
if nums[i] > nums[maxId] { maxId = i } | |
} | |
var output: [Int] = [] | |
output.append(nums[maxId]) | |
for i in k..<len { | |
cleanDeque(i, k) | |
deque.append(i) | |
if let first = deque.first { | |
output.append(nums[first]) | |
} | |
} | |
return output | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment