-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsliding_window.go
63 lines (57 loc) · 899 Bytes
/
sliding_window.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
var n int
var k int
fmt.Scanf("%d%d", &n, &k)
q := make([]int, n+1)
//sanner read n int
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
a := make([]int, 0)
for scanner.Scan() {
if scanner.Text() != "" {
val, _ := strconv.Atoi(scanner.Text())
a = append(a, val)
}
}
if scanner.Err() != nil {
fmt.Printf("error: %s\n", scanner.Err())
}
hh := 0
tt := -1
for i := 0; i < n; i++ {
for hh <= tt && q[hh] < i-k+1 {
hh++
}
for hh <= tt && a[q[tt]] >= a[i] {
tt--
}
tt++
q[tt] = i
if i >= k-1 {
fmt.Printf("%d ", a[q[hh]])
}
}
fmt.Println("")
hh = 0
tt = -1
for i := 0; i < n; i++ {
for hh <= tt && q[hh] < i-k+1 {
hh++
}
for hh <= tt && a[q[tt]] <= a[i] {
tt--
}
tt++
q[tt] = i
if i >= k-1 {
fmt.Printf("%d ", a[q[hh]])
}
}
}