-
Notifications
You must be signed in to change notification settings - Fork 3
/
335.cpp
40 lines (37 loc) · 1.51 KB
/
335.cpp
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
class Solution {
public:
bool isSelfCrossing(vector<int>& distance) {
for (int i = 3; i < distance.size(); ++i) {
// case 1
// x(i-2)
// ┌───┐
// x(i-1)│ │x(i-3)
// └───┼──>
// x(i)│
if (distance[i] >= distance[i - 2] && distance[i - 1] <= distance[i - 3]) return true;
// case 2
// x(i-3)
// ┌──────┐
// │ │x(i-4)
// x(i-2)│ ^
// │ │x(i)
// └──────│
// x(i-1)
if (i >= 4) {
if (distance[i - 1] == distance[i - 3] && distance[i] + distance[i - 4] >= distance[i - 2]) return true;
}
// case 3
// x(i-4) x(i-5)
// ┌────────┐ /
// │ │/
// x(i-3)│ <│──────│
// │ x(i) │x(i-1)
// └───────────────│
// x(i-2)
if (i >= 5) {
if (distance[i - 3] > distance[i - 1] && distance[i - 2] > distance[i - 4] && distance[i] + distance[i - 4] >= distance[i - 2] && distance[i - 5] + distance[i - 1] >= distance[i - 3]) return true;
}
}
return false;
}
};