forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
self-crossing.cpp
38 lines (36 loc) Β· 1.11 KB
/
self-crossing.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
// Time: O(n)
// Space: O(1)
class Solution {
public:
bool isSelfCrossing(vector<int>& x) {
if (x.size() >= 5 && x[3] == x[1] && x[4] + x[0] >= x[2]) {
// Crossing in a loop:
// 2
// 3 ββββββ
// ββββ>β1
// 4 0 (overlapped)
return true;
}
for (int i = 3; i < x.size(); ++i) {
if (x[i] >= x[i - 2] && x[i - 3] >= x[i - 1]) {
// Case 1:
// i-2
// i-1βββ
// βββΌβ>i
// i-3
return true;
} else if (i >= 5 && x[i - 4] <= x[i - 2] && x[i] + x[i - 4] >= x[i - 2] &&
x[i - 1] <= x[i - 3] && x[i - 1] + x[i - 5] >= x[i - 3]) {
// Case 2:
// i-4
// ββββ
// βi<βΌββ
// i-3β i-5βi-1
// ββββββ
// i-2
return true;
}
}
return false;
}
};