Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“SlidingWindowMaximum” error #276

Open
piglikeYoung opened this issue Nov 4, 2019 · 0 comments
Open

“SlidingWindowMaximum” error #276

piglikeYoung opened this issue Nov 4, 2019 · 0 comments

Comments

@piglikeYoung
Copy link

piglikeYoung commented Nov 4, 2019

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
    }
@piglikeYoung piglikeYoung changed the title SlidingWindowMaximum Code error “SlidingWindowMaximum” error Nov 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant