“SlidingWindowMaximum” error
piglikeYoung opened this issue · 0 comments
piglikeYoung commented
https://github.com/soapyigu/LeetCode-Swift/blob/master/Array/SlidingWindowMaximum.swift
testcase: [ 2, 3, 4, 2, 6, 2, 5, 1 ]
expected: [4, 4, 6, 6, 6, 5]
errorResult: [4, 4, 6, 6, 5, 5]
Modify
func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
guard k > 0 && k <= nums.count else {
return []
}
var maxIdx = [Int]()
var res = [Int]()
for i in 0..<nums.count {
while maxIdx.count > 0 && nums[maxIdx.last!] <= nums[i] {
maxIdx.removeLast()
}
maxIdx.append(i)
if i >= k - 1 {
if maxIdx.first! + k == i {
maxIdx.removeFirst()
}
res.append(nums[maxIdx.first!])
}
}
return res
}