-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path335-SelfCrossing.go
More file actions
121 lines (108 loc) · 3.84 KB
/
335-SelfCrossing.go
File metadata and controls
121 lines (108 loc) · 3.84 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
// 335. Self Crossing
// You are given an array of integers distance.
// You start at the point (0, 0) on an X-Y plane, and you move distance[0] meters to the north, then distance[1] meters to the west, distance[2] meters to the south, distance[3] meters to the east, and so on.
// In other words, after each move, your direction changes counter-clockwise.
// Return true if your path crosses itself or false if it does not.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/12/21/11.jpg" />
// Input: distance = [2,1,1,2]
// Output: true
// Explanation: The path crosses itself at the point (0, 1).
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/12/21/22.jpg" />
// Input: distance = [1,2,3,4]
// Output: false
// Explanation: The path does not cross itself at any point.
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2022/12/21/33.jpg" />
// Input: distance = [1,1,1,2,1]
// Output: true
// Explanation: The path crosses itself at the point (0, 0).
// Constraints:
// 1 <= distance.length <= 10^5
// 1 <= distance[i] <= 10^5
import "fmt"
func isSelfCrossing(distance []int) bool {
distance = append([]int{0, 0, 0, 0}, distance...)
n, i := len(distance), 4
for i < n && distance[i] > distance[i-2] {
i++
}
if i == n {
return false
}
if distance[i] >= distance[i-2]-distance[i-4] {
distance[i-1] -= distance[i-3]
}
i = i + 1
for i < n && distance[i] < distance[i-2] {
i++
}
return i != n
}
func isSelfCrossing1(distance []int) bool {
n := len(distance)
for i := 3; i < n; i++ {
if distance[i] >= distance[i-2] && distance[i-1] <= distance[i-3] {
return true
}
if i < 4 {
continue
}
if distance[i-1] == distance[i-3] && distance[i]+distance[i-4] >= distance[i-2] {
return true
}
if i < 5 {
continue
}
if distance[i] >= distance[i-2] - distance[i-4] &&
distance[i-1] >= distance[i-3] - distance[i-5] &&
distance[i-1] <= distance[i-3] &&
distance[i-2] >= distance[i-4] &&
distance[i-3] >= distance[i-5] {
return true
}
}
return false
}
func isSelfCrossing2(d []int) bool {
for i := 3; i < len(d); i++ {
if d[i] >= d[i-2] && d[i-1] <= d[i-3] {
return true
}
if i >= 4 && d[i-1] == d[i-3] && d[i]+d[i-4] >= d[i-2] {
return true
}
if i >= 5 && d[i-2] >= d[i-4] && d[i-1] <= d[i-3] && d[i] >= d[i-2]-d[i-4] && d[i-1]+d[i-5] >= d[i-3] {
return true
}
}
return false
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/12/21/11.jpg" />
// Input: distance = [2,1,1,2]
// Output: true
// Explanation: The path crosses itself at the point (0, 1).
fmt.Println(isSelfCrossing([]int{ 2,1,1,2 })) // true
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/12/21/22.jpg" />
// Input: distance = [1,2,3,4]
// Output: false
// Explanation: The path does not cross itself at any point.
fmt.Println(isSelfCrossing([]int{ 1,2,3,4 })) // false
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2022/12/21/33.jpg" />
// Input: distance = [1,1,1,2,1]
// Output: true
// Explanation: The path crosses itself at the point (0, 0).
fmt.Println(isSelfCrossing([]int{ 1,1,1,2,1 })) // true
fmt.Println(isSelfCrossing1([]int{ 2,1,1,2 })) // true
fmt.Println(isSelfCrossing1([]int{ 1,2,3,4 })) // false
fmt.Println(isSelfCrossing1([]int{ 1,1,1,2,1 })) // true
fmt.Println(isSelfCrossing2([]int{ 2,1,1,2 })) // true
fmt.Println(isSelfCrossing2([]int{ 1,2,3,4 })) // false
fmt.Println(isSelfCrossing2([]int{ 1,1,1,2,1 })) // true
}