Skip to content

Commit 43dc4e3

Browse files
committedFeb 22, 2025
Add solution #335
1 parent 0731285 commit 43dc4e3

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
331|[Verify Preorder Serialization of a Binary Tree](./0331-verify-preorder-serialization-of-a-binary-tree.js)|Medium|
271271
332|[Reconstruct Itinerary](./0332-reconstruct-itinerary.js)|Hard|
272272
334|[Increasing Triplet Subsequence](./0334-increasing-triplet-subsequence.js)|Medium|
273+
335|[Self Crossing](./0335-self-crossing.js)|Hard|
273274
337|[House Robber III](./0337-house-robber-iii.js)|Medium|
274275
338|[Counting Bits](./0338-counting-bits.js)|Easy|
275276
341|[Flatten Nested List Iterator](./0341-flatten-nested-list-iterator.js)|Medium|

‎solutions/0335-self-crossing.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 335. Self Crossing
3+
* https://leetcode.com/problems/self-crossing/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array of integers distance.
7+
*
8+
* You start at the point (0, 0) on an X-Y plane, and you move distance[0] meters to the north, then
9+
* distance[1] meters to the west, distance[2] meters to the south, distance[3] meters to the east,
10+
* and so on. In other words, after each move, your direction changes counter-clockwise.
11+
*
12+
* Return true if your path crosses itself or false if it does not.
13+
*/
14+
15+
/**
16+
* @param {number[]} d
17+
* @return {boolean}
18+
*/
19+
var isSelfCrossing = function(d) {
20+
for (let i = 3; i < d.length; i++) {
21+
if (d[i] >= d[i - 2] && d[i - 1] <= d[i - 3]) {
22+
return true;
23+
}
24+
if (i >= 4 && d[i - 1] === d[i - 3] && d[i] + d[i - 4] >= d[i - 2]) {
25+
return true;
26+
}
27+
if (i >= 5 && d[i - 2] >= d[i - 4] && d[i] + d[i - 4] >= d[i - 2]
28+
&& d[i - 1] <= d[i - 3] && d[i - 1] + d[i - 5] >= d[i - 3]) {
29+
return true;
30+
}
31+
}
32+
return false;
33+
};

0 commit comments

Comments
 (0)
Please sign in to comment.