Skip to content

Commit

Permalink
Go: solution for 239-Sliding-Window-Maximum.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushi Panchariya committed Sep 29, 2022
1 parent b62784a commit 2d343f6
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions go/239-Sliding-Window-Maximum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
func maxSlidingWindow(nums []int, k int) []int {
// get top/last element of queue: q[len(q)-1]
// pop/remove from the top/last element of queue: q[:len(q)-1]
// remove left value from queue: q[1:]
output := []int{}
q := make([]int, 0)
l, r := 0, 0

for r < len(nums) {
// pop smaller values from q
for len(q) != 0 && nums[q[len(q)-1]] < nums[r] {
q = q[:len(q)-1]
}
q = append(q, r)

// remove left val from window
if l > q[0] {
q = q[1:]
}

if (r + 1) >= k {
output = append(output, nums[q[0]])
l++
}
r++
}
return output
}

0 comments on commit 2d343f6

Please sign in to comment.