File tree 2 files changed +72
-0
lines changed
src/main/java/com/fishercoder/solutions
2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change @@ -264,6 +264,7 @@ Your ideas/fixes/algorithms are more than welcome!
264
264
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_338.java)| O(nlogn)|O(h) | Medium|
265
265
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_337.java)| O(n)|O(n)| Medium | DP
266
266
|336|[Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_336.java)| O(n^2)|O(n) | Hard|
267
+ |335|[Self Crossing](https://leetcode.com/problems/self-crossing/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_335.java)| O(n)|O(1) | Hard| Math
267
268
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_334.java)| O(n^2)|O(1) | Medium|
268
269
|333|[Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_333.java)| O(n)|O(n) | Medium| Tree
269
270
|332|[Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_332.java)| O(n)|O(n) | Medium| Graph, DFS
Original file line number Diff line number Diff line change
1
+ package com.fishercoder.solutions;
2
+
3
+ /**
4
+ * 335. Self Crossing
5
+ *
6
+ * You are given an array x of n positive numbers.
7
+ * You start at point (0,0) and moves x[0] metres to the north,
8
+ * then x[1] metres to the west,
9
+ * x[2] metres to the south,
10
+ * x[3] metres to the east and so on.
11
+ * In other words, after each move your direction changes counter-clockwise.
12
+
13
+ Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not.
14
+
15
+ Example 1:
16
+ Given x =
17
+ [2, 1, 1, 2]
18
+ ,
19
+ ?????
20
+ ? ?
21
+ ???????>
22
+ ?
23
+
24
+ Return true (self crossing)
25
+ Example 2:
26
+ Given x =
27
+ [1, 2, 3, 4]
28
+ ,
29
+ ????????
30
+ ? ?
31
+ ?
32
+ ?
33
+ ?????????????>
34
+
35
+ Return false (not self crossing)
36
+ Example 3:
37
+ Given x =
38
+ [1, 1, 1, 1]
39
+ ,
40
+ ?????
41
+ ? ?
42
+ ?????>
43
+
44
+ Return true (self crossing)
45
+
46
+ */
47
+ public class _335 {
48
+
49
+ /**reference: https://discuss.leetcode.com/topic/38014/java-oms-with-explanation/2*/
50
+ public boolean isSelfCrossing(int[] x) {
51
+ int l = x.length;
52
+ if (l <= 3) return false;
53
+
54
+ for (int i = 3; i < l; i++) {
55
+ if (x[i] >= x[i - 2] && x[i - 1] <= x[i - 3]) {
56
+ return true; //Fourth line crosses first line and onward
57
+ }
58
+ if (i >= 4) {
59
+ if (x[i - 1] == x[i - 3] && x[i] + x[i - 4] >= x[i - 2]) {
60
+ return true; // Fifth line meets first line and onward
61
+ }
62
+ }
63
+ if (i >= 5) {
64
+ if (x[i - 2] - x[i - 4] >= 0 && x[i] >= x[i - 2] - x[i - 4] && x[i - 1] >= x[i - 3] - x[i - 5] && x[i - 1] <= x[i - 3]) {
65
+ return true; // Sixth line crosses first line and onward
66
+ }
67
+ }
68
+ }
69
+ return false;
70
+ }
71
+ }
You can’t perform that action at this time.
0 commit comments