1
class Solution {
2
public:
3
bool isSelfCrossing(vector<int> &distance) {
4
if (distance.size() <= 3) return false; // only can have intersection with more than 4 lines
5

6
distance.insert(distance.begin(),
7
0); // for the edge case: line i intersect with line i-4 at (0, 0)
8
for (int i = 3; i < distance.size(); i++) {
9
// check line i-3
10
if (distance[i - 2] <= distance[i] && distance[i - 1] <= distance[i - 3]) return true;
11

12
// check line i-5
13
if (i >= 5) {
14
if (distance[i - 1] <= distance[i - 3] &&
15
distance[i - 1] >= distance[i - 3] - distance[i - 5] &&
16
distance[i - 2] >= distance[i - 4] && distance[i - 2] <= distance[i - 4] + distance[i])
17
return true;
18
}
19
}
20
return false;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0