-
Notifications
You must be signed in to change notification settings - Fork 0
/
335.go
47 lines (43 loc) · 1.08 KB
/
335.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
package p335
/**
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north,
then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on.
In other words, after each move your direction changes counter-clockwise.
Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not.
*/
func isSelfCrossing(x []int) bool {
if len(x) < 4 {
return false
}
trapLo, trapHi, trapIn := 0, 0, -1
for i := 3; i < len(x); i++ {
if x[i-1] <= x[i-3] && x[i] >= x[i-2] {
return true
}
if trapIn >= 0 {
if x[i] >= trapIn {
return true
} else {
trapLo, trapHi, trapIn = 0, 0, -1
}
}
if trapLo > 0 {
if x[i-1] == x[i-3] && x[i] >= trapLo {
return true
}
if x[i] >= trapLo && x[i] <= trapHi {
trapIn = x[i-1] - x[i-3]
} else if x[i] < trapIn {
trapLo, trapHi = 0, 0
} else {
trapLo = x[i-1] - x[i-3]
trapHi = x[i-1]
}
}
if x[i-1] >= x[i-3] && x[i] >= x[i-2] {
trapLo = x[i-1] - x[i-3]
trapHi = x[i-1]
}
}
return false
}