Skip to content

Commit 40129d1

Browse files
author
beyondyyh
committed
add maxSlidingWindow
1 parent 15c34f0 commit 40129d1

File tree

5 files changed

+184
-2
lines changed

5 files changed

+184
-2
lines changed

algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@
7373
|22|[链表中倒数第k个节点](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/)|[go](./offer100/22.getKthFromEnd.go)|S|
7474
|40|[最小的k个数](https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/)|[go](./offer100/40.getLeastNumbers.go)|S|
7575
|58|[左旋转字符串](https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/)|[go](./offer100/58.reverseLeftWords.go)|S|
76+
|239|[滑动窗口最大值](https://leetcode-cn.com/problems/sliding-window-maximum/)|[go](./offer100/239.maxSlidingWindow.go)|M|
7677
||||[:top:](#Top)|

algorithms/offer100/22.getKthFromEnd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Test_getKthFromEnd(t *testing.T) {
2222
Input: []interface{}{
2323
[]int{1, 2, 3, 4, 5}, 4,
2424
},
25-
Expected: []int{1, 2, 3, 4, 5},
25+
Expected: []int{2, 3, 4, 5},
2626
},
2727
}
2828

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package offer100
2+
3+
// 给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
4+
// 返回滑动窗口中的最大值。
5+
// 示例 1:
6+
// 输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
7+
// 输出:[3,3,5,5,6,7]
8+
// 解释:
9+
// 滑动窗口的位置 最大值
10+
// --------------- -----
11+
// [1 3 -1] -3 5 3 6 7 3
12+
// 1 [3 -1 -3] 5 3 6 7 3
13+
// 1 3 [-1 -3 5] 3 6 7 5
14+
// 1 3 -1 [-3 5 3] 6 7 5
15+
// 1 3 -1 -3 [5 3 6] 7 6
16+
// 1 3 -1 -3 5 [3 6 7] 7
17+
// Leetcode :https://leetcode-cn.com/problems/sliding-window-maximum
18+
19+
// 可以采用最大堆的数据结构来保存元素,堆顶元素即为当前堆的最大值,并判断当前堆顶元素这是否在窗口中,
20+
// 在则直接返回,不在则利用堆特性删除堆顶元素并调整堆
21+
// 时间复杂度:O(N logN), n=len(nums)
22+
23+
import (
24+
"container/heap"
25+
"fmt"
26+
)
27+
28+
type Item struct {
29+
index int // 元素下标
30+
value int // 元素值
31+
}
32+
33+
// 声名一个优先队列,java的优先队列就是用堆实现的
34+
type PriorityQueue []*Item
35+
36+
// 实现heap接口的 Len,Less,Swap,Push,Pop 方法
37+
func (pq PriorityQueue) Len() int {
38+
return len(pq)
39+
}
40+
41+
// 比较器,当两者的值相同时,比较下标的位置
42+
func (pq PriorityQueue) Less(i, j int) bool {
43+
if pq[i].value == pq[j].value {
44+
return pq[i].index > pq[j].index
45+
}
46+
return pq[i].value > pq[j].value
47+
}
48+
49+
func (pq PriorityQueue) Swap(i, j int) {
50+
pq[i], pq[j] = pq[j], pq[i]
51+
}
52+
53+
// Push、Pop不仅改变堆的值还改变堆长度,所以receiver用指针
54+
func (pq *PriorityQueue) Push(x interface{}) {
55+
// n := len(*pq)
56+
item := x.(*Item)
57+
// item.index = n
58+
*pq = append(*pq, item)
59+
}
60+
61+
func (pq *PriorityQueue) Pop() interface{} {
62+
old := *pq
63+
n := len(old)
64+
item := old[n-1]
65+
old[n-1] = nil // 避免内存泄露
66+
*pq = old[0 : n-1]
67+
return item
68+
}
69+
70+
// 只查看堆顶元素,不更改堆的结构,此处是大顶堆所以第一个元素最大
71+
func (pq PriorityQueue) peek() interface{} {
72+
return pq[0]
73+
}
74+
75+
func dump(pq PriorityQueue) {
76+
nums := make([]Item, 0)
77+
for _, v := range pq {
78+
nums = append(nums, *v)
79+
}
80+
fmt.Printf("pq: %+v\n", nums)
81+
}
82+
83+
// -----
84+
func maxSlidingWindow(nums []int, k int) []int {
85+
// 初始化前K的元素到堆中
86+
pq := make(PriorityQueue, k)
87+
for i := 0; i < k; i++ {
88+
pq[i] = &Item{index: i, value: nums[i]}
89+
}
90+
heap.Init(&pq)
91+
// dump(pq)
92+
93+
n := len(nums)
94+
// 总共有n-k+1个窗口,声名一个长度长度&容量n-k+1的slice
95+
ans := make([]int, n-k+1)
96+
// 堆顶元素即是第一个窗口最大值,先放进ans
97+
ans[0] = pq.peek().(*Item).value
98+
99+
// 遍历将剩下的元素依次入堆
100+
for i := k; i < n; i++ {
101+
// 将新元素入堆
102+
item := &Item{index: i, value: nums[i]}
103+
heap.Push(&pq, item)
104+
// 循环判断当前堆顶是否在窗口中,一般思路是遍历窗口元素与堆顶进行对比,时间复杂度为O(k)
105+
// 反向思维:堆顶元素已经是最大值,可以依次pop比较堆顶元素的下标是否小于窗口的左边界i-k+1,直到堆为空或者堆顶元素下标等于左边界,出栈时间复杂度O(1)
106+
for pq.Len() > 0 && pq.peek().(*Item).index <= i-k {
107+
heap.Pop(&pq)
108+
}
109+
// 在窗口中直接赋值即可
110+
if pq.Len() > 0 {
111+
ans[i-k+1] = pq.peek().(*Item).value
112+
}
113+
}
114+
// dump(pq)
115+
return ans
116+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package offer100
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type (
9+
entry239input struct {
10+
nums []int
11+
k int
12+
}
13+
entry239 struct {
14+
name string
15+
input entry239input
16+
expected []int
17+
}
18+
)
19+
20+
// run: go test -run Test_maxSlidingWindow
21+
func Test_maxSlidingWindow(t *testing.T) {
22+
cases := []entry239{
23+
{
24+
name: "x1",
25+
input: entry239input{
26+
nums: []int{1, 3, -1, -3, 5, 3, 6, 7},
27+
k: 3,
28+
},
29+
expected: []int{3, 3, 5, 5, 6, 7},
30+
},
31+
{
32+
name: "x2",
33+
input: entry239input{
34+
nums: []int{1},
35+
k: 1,
36+
},
37+
expected: []int{1},
38+
},
39+
{
40+
name: "x3",
41+
input: entry239input{
42+
nums: []int{1, -1},
43+
k: 1,
44+
},
45+
expected: []int{1, -1},
46+
},
47+
{
48+
name: "x4",
49+
input: entry239input{
50+
nums: []int{9, 11},
51+
k: 2,
52+
},
53+
expected: []int{11},
54+
},
55+
}
56+
57+
for _, c := range cases {
58+
t.Run(c.name, func(t *testing.T) {
59+
output := maxSlidingWindow(c.input.nums, c.input.k)
60+
if !reflect.DeepEqual(output, c.expected) {
61+
t.Errorf("maxSlidingWindow(%v,%d)=%v, expected=%v", c.input.nums, c.input.k, output, c.expected)
62+
}
63+
})
64+
}
65+
}

algorithms/offer100/40.getLeastNumbers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66
)
77

8-
// run: go test -run -v Test_getLeastNumbers
8+
// run: go test -run Test_getLeastNumbers
99
func Test_getLeastNumbers(t *testing.T) {
1010
cases := []CaseEntry{
1111
{

0 commit comments

Comments
 (0)