Golang Test is broken, TLE for O(n) solution.


  • 0
    D
    func maxSlidingWindow(nums []int, k int) []int {
        if len(nums) == 0 {
            return nil
        }
        var q,ans []int
        
        for i:=0; i< len(nums); i++ {
            for len(q) != 0 && nums[q[len(q)-1]] < nums[i] {
                q = q[:len(q)-1]
            }
            q = append(q,i)
            
            if i < k-1 {
                continue
            }
            ans = append(ans,nums[q[0]])
            if q[0] == i-k+1 {
                q = q[1:]
            }
        }
        return ans
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.