[LeetCode] Self Crossing

335. Self Crossing

You are given an array of integers distance.

You start at 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, and false if it does not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
bool isSelfCrossing(vector<int>& distance) {
int b = 0, c = 0, d = 0, e = 0, f = 0;
for(auto& a : distance) {
if(d > 0 and a >= c and b <= d) return true;
if(e > 0 and d == b and e + a >= c) return true;
if(f > 0 and f + b >= d and b <= d and e + a >= c and e <= c) return true;

f = e, e = d, d = c, c = b, b = a;
}
return false;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/24/PS/LeetCode/self-crossing/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.