Self Crossing
The given problem describes a scenario where you’re moving on an XY plane based on the given distances. You start at (0, 0), move north, west, south, and east in a cyclic manner. The task is to find out if the path crosses itself.
The scenario where crossing occurs can be broken down into a few cases:
 Case 1: When the 4th line crosses the 1st line (like the first example).
 Case 2: When the 5th line crosses the 1st line (like a pentagon with a shorter side).
 Case 3: When the 6th line crosses the 1st line.
We can translate these cases into conditions and check them as we iterate through the distances.
Here’s a code snippet that applies the logic:


Key Takeaways
 The problem can be solved by carefully analyzing the conditions that lead to selfcrossing.
 The above code snippet efficiently checks for these conditions, using constant space and linear time.
 The time complexity of this solution is (O(n)), and the space complexity is (O(1)), where (n) is the length of the
distance
array.


Identifying Problem Isomorphism
“Self Crossing” can be mapped to “Robot Bounded In Circle”.
“Self Crossing” involves a person moving in a plane along a given path. The task is to determine if the path of the person crosses itself at any point. This requires processing a series of steps and keeping track of the path traversed to check for intersections.
In “Robot Bounded In Circle”, there is a robot moving in a plane according to certain instructions. The goal is to determine if the final position of the robot after executing the instructions is in a bounded circle. This involves processing a series of steps and keeping track of the robot’s position and direction at each step.
The reason is that both problems deal with tracking movements in a plane and checking for a certain condition (selfcrossing or remaining within a circle). However, the “Self Crossing” problem is more complex as it requires checking for intersections with any part of the path, while the “Robot Bounded In Circle” problem only checks if the robot’s movements are constrained within a certain area.
10 Prerequisite LeetCode Problems
“335. Self Crossing” deals with array manipulation and conditional checks within a complex pattern. Here are 10 problems to prepare for it:
“283. Move Zeroes”  Basic problem for learning how to manipulate elements in an array.
“485. Max Consecutive Ones”  Simple problem to get used to array iteration and conditions.
“88. Merge Sorted Array”  A bit more complex manipulation with two arrays.
“75. Sort Colors”  This problem makes you handle three possible values, and not just two.
“448. Find All Numbers Disappeared in an Array”  More complex manipulation and a bit more complex conditions.
“41. First Missing Positive”  It has a tricky part that is similar to the idea of cycles in arrays.
“152. Maximum Product Subarray”  This problem introduces you to the idea of keeping track of multiple possible outcomes.
“769. Max Chunks To Make Sorted”  This problem could help you to better understand how to divide the array based on the conditions inside it.
“287. Find the Duplicate Number”  Complex problem about cycles in the array, but the conditions are simpler than in problem 335.
“189. Rotate Array”  A more complex problem about handling the elements of the array in a cyclic way.
This list includes problems of increasing complexity, starting with basic array manipulations and ending with complex conditions and manipulations.


Problem Classification
Self Crossing  This problem asks to check if a moving line on a grid crosses itself. This is a Movement Tracking Problem.
Language Agnostic Coding Drills
Length Check: The initial step is checking whether the length of the array is less than or equal to 3. If so, the function should return
false
immediately. This serves as an edgecase handler for the algorithm.Loop Through Elements: This solution utilizes a
for loop
that begins from the third index (i.e.,i = 3
) up to the last index of the arrayx
. This is to perform checks on each item and its neighboring items.Crossing Checks: The subsequent three steps involve specific checks to determine whether a path crosses itself. Here are the three conditions, in order of complexity:
 Check if the current step is equal to or larger than the step two steps before, and if the step immediately before is equal to or smaller than the step three steps before. If this is true, return
true
.  If
i >= 4
(meaning we’re at least four steps in), check if the previous step is equal to the step three steps before and if the step two steps before is equal to or smaller than the current step plus the step four steps before. If this condition is met, returntrue
.  If
i >= 5
(meaning we’re at least five steps in), check a set of conditions: if the step four steps before is equal to or smaller than the step two steps before, and if the step two steps before is equal to or smaller than the current step plus the step four steps before, and if the previous step is equal to or smaller than the step three steps before, and if the step three steps before is equal to or smaller than the previous step plus the step five steps before. If all these conditions are met, returntrue
.
 Check if the current step is equal to or larger than the step two steps before, and if the step immediately before is equal to or smaller than the step three steps before. If this is true, return
Return
False
: If none of the above conditions are met after checking all items in the array, the function should returnfalse
.
To implement the final solution:
 First, master how to check the length of an array and return a value based on the length.
 Second, learn how to traverse arrays in a loop and access the elements at different indices.
 Third, learn how to implement complex logical conditions with multiple parts and comparisons.
 Lastly, learn how to return a default value if no conditions in a set of checks are met.
By tackling these drills individually and then combining them, you can understand the original solution better and apply these concepts to similar problems.
Targeted Drills in Python
 Length Check:


 Loop Through Elements:


 Crossing Checks:
Here are the three conditions, in order of complexity:
 Check if the current step is equal to or larger than the step two steps before, and if the step immediately before is equal to or smaller than the step three steps before.


 If
i >= 4
(meaning we’re at least four steps in), check if the previous step is equal to the step three steps before and if the step two steps before is equal to or smaller than the current step plus the step four steps before.


 If
i >= 5
(meaning we’re at least five steps in), check a set of conditions.


 Return
False
:


These drills target different aspects of the original problem. By combining all these drills, one can recreate the original solution.