All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0201_0300.s0239_sliding_window_maximum.solution.ts Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Heap_Priority_Queue
// #Sliding_Window #Queue #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
// #2023_10_06_Time_216_ms_(99.50%)_Space_71.4_MB_(89.85%)

function maxSlidingWindow(nums: number[], k: number): number[] {
    const result: number[] = []
    // Maintain indices in the deque
    const deque: number[] = []
    for (let i = 0; i < nums.length; i++) {
        // Remove indices that are out of the window
        while (deque.length > 0 && deque[0] <= i - k) {
            deque.shift()
        }
        // Remove indices of smaller elements as they are no longer candidates for maximum
        while (deque.length > 0 && nums[i] >= nums[deque[deque.length - 1]]) {
            deque.pop()
        }
        // Add the current index to the deque
        deque.push(i)
        // Add the maximum element for the current window to the result
        if (i >= k - 1) {
            result.push(nums[deque[0]])
        }
    }
    return result
}

export { maxSlidingWindow }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy