Created
January 6, 2026 15:49
-
-
Save rtsoy/0e1cb851c816cdcd98af4ddcb30f6b98 to your computer and use it in GitHub Desktop.
239. Sliding Window Maximum
This file contains hidden or 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
| // https://leetcode.com/problems/sliding-window-maximum | |
| // | |
| // Time: O(n) | |
| // Space: O(n) | |
| // | |
| // n = number of elements in array | |
| // .................... // | |
| func maxSlidingWindow(nums []int, k int) []int { | |
| n := len(nums) | |
| res := make([]int, 0) | |
| deque := make([]int, 0) | |
| l, r := 0, 0 | |
| for r < n { | |
| for len(deque) > 0 && nums[r] > nums[deque[len(deque)-1]] { | |
| deque = deque[:len(deque)-1] | |
| } | |
| deque = append(deque, r) | |
| if (r-l+1) == k { | |
| res = append(res, nums[deque[0]]) | |
| if nums[deque[0]] == nums[l] { | |
| deque = deque[1:] | |
| } | |
| l++ | |
| } | |
| r++ | |
| } | |
| return res | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment