Skip to content

Commit 2d343f6

Browse files
author
Rushi Panchariya
committed
Go: solution for 239-Sliding-Window-Maximum.go
1 parent b62784a commit 2d343f6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: go/239-Sliding-Window-Maximum.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func maxSlidingWindow(nums []int, k int) []int {
2+
// get top/last element of queue: q[len(q)-1]
3+
// pop/remove from the top/last element of queue: q[:len(q)-1]
4+
// remove left value from queue: q[1:]
5+
output := []int{}
6+
q := make([]int, 0)
7+
l, r := 0, 0
8+
9+
for r < len(nums) {
10+
// pop smaller values from q
11+
for len(q) != 0 && nums[q[len(q)-1]] < nums[r] {
12+
q = q[:len(q)-1]
13+
}
14+
q = append(q, r)
15+
16+
// remove left val from window
17+
if l > q[0] {
18+
q = q[1:]
19+
}
20+
21+
if (r + 1) >= k {
22+
output = append(output, nums[q[0]])
23+
l++
24+
}
25+
r++
26+
}
27+
return output
28+
}

0 commit comments

Comments
 (0)