Skip to content

Commit fd6e47e

Browse files
committed
added problem 239
1 parent efa1c54 commit fd6e47e

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package problem0239
2+
3+
import "container/heap"
4+
5+
/*
6+
You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right.
7+
You can only see the k numbers in the window.
8+
Each time the sliding window moves right by one position.
9+
Return the max sliding window.
10+
*/
11+
12+
func maxSlidingWindow(nums []int, k int) []int {
13+
var pq = MakeHeap(MaxHeap)
14+
var res = make([]int, len(nums)-k+1)
15+
16+
for i := 0; i < k; i++ {
17+
heap.Push(pq, [2]int{nums[i], i})
18+
}
19+
20+
res[0] = pq.Peek()[0]
21+
for i := k; i < len(nums); i++ {
22+
for pq.Len() != 0 && pq.Peek()[1] <= i-k {
23+
heap.Pop(pq)
24+
}
25+
heap.Push(pq, [2]int{nums[i], i})
26+
res[i-k+1] = pq.Peek()[0]
27+
}
28+
return res
29+
}
30+
31+
type Heap struct {
32+
Values [][2]int
33+
LessFunc func([2]int, [2]int) bool
34+
}
35+
36+
func (h *Heap) Less(i, j int) bool { return h.LessFunc(h.Values[i], h.Values[j]) }
37+
func (h *Heap) Swap(i, j int) { h.Values[i], h.Values[j] = h.Values[j], h.Values[i] }
38+
func (h *Heap) Len() int { return len(h.Values) }
39+
func (h *Heap) Peek() [2]int { return h.Values[0] }
40+
func (h *Heap) Pop() (v interface{}) {
41+
h.Values, v = h.Values[:h.Len()-1], h.Values[h.Len()-1]
42+
return v
43+
}
44+
func (h *Heap) Push(v interface{}) { h.Values = append(h.Values, v.([2]int)) }
45+
46+
func MakeHeap(less func([2]int, [2]int) bool) *Heap {
47+
return &Heap{LessFunc: less}
48+
}
49+
50+
func MaxHeap(i, j [2]int) bool { return i[0] > j[0] }
51+
func MinHeap(i, j [2]int) bool { return i[0] < j[0] }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problem0239
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type TestCase struct {
11+
Input []int
12+
K int
13+
Expected []int
14+
}
15+
16+
var TestCases = []TestCase{
17+
{[]int{1, 3, -1, -3, 5, 3, 6, 7}, 3, []int{3, 3, 5, 5, 6, 7}},
18+
{[]int{1}, 1, []int{1}},
19+
}
20+
21+
func TestMaxSlidingWindow(t *testing.T) {
22+
assert := assert.New(t)
23+
24+
for _, tc := range TestCases {
25+
want := tc.Expected
26+
got := maxSlidingWindow(tc.Input, tc.K)
27+
assert.Equal(want, got, fmt.Sprintf("%+v", tc))
28+
}
29+
}
30+
31+
func BenchmarkMaxSlidingWindow(b *testing.B) {
32+
for i := 0; i < b.N; i++ {
33+
for _, tc := range TestCases {
34+
maxSlidingWindow(tc.Input, tc.K)
35+
}
36+
}
37+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ Each problem is in it's own directory, with test files. There are helper package
158158
| 0235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) | [My Solution](./problems/problem0235) ||
159159
| 0236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree) | [My Solution](./problems/problem0236) ||
160160
| 0237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list) | [My Solution](./problems/problem0237) ||
161+
| 0239 | [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum) | [My Solution](./problems/problem0239) ||
161162
| 0240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [My Solution](./problems/problem0240) ||
162163
| 0242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram) | [My Solution](./problems/problem0242) ||
163164
| 0258 | [Add Digits](https://leetcode.com/problems/add-digits) | [My Solution](./problems/problem0258) ||

0 commit comments

Comments
 (0)