-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path59-I.go
70 lines (64 loc) · 1.14 KB
/
59-I.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
/***
"题目:**滑动窗口的最大值**
[滑动窗口的最大值](https://leetcode-cn.com/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof)
题目描述:
***/
/**
解法一
说明:
**/
func maxSlidingWindow(nums []int, k int) []int {
if len(nums) == 0 || k <= 0 || k > len(nums) {
return nil
}
var maxNums []int
max := -1
for i := 0; i <= len(nums)-k; i++ {
l := i
r := i + k - 1
if max == -1 || max == l-1 {
max = getMax(nums, l, r)
} else {
if nums[r] > nums[max] {
max = r
}
}
maxNums = append(maxNums, nums[max])
}
return maxNums
}
func maxSlidingWindow2(nums []int, k int) []int {
if len(nums) == 0 || k <= 0 || k > len(nums) {
return nil
}
var maxNums []int
var max int
for i, j := 0, k-1; j >= 0 && j < len(nums); j++ {
if i == 0 || max == nums[i-1] {
max = nums[i]
for t := j; t > i; t-- {
if max < nums[t] {
max = nums[t]
}
}
} else {
if nums[j] > max {
max = nums[j]
}
}
maxNums = append(maxNums, max)
i++
}
return maxNums
}
func getMax(nums []int, l, r int) int {
for l < r {
if nums[l] > nums[r] {
r--
} else {
l++
}
}
return l
}