-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_735.java
163 lines (157 loc) · 5.96 KB
/
_735.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.fishercoder.solutions.firstthousand;
import java.util.Deque;
import java.util.LinkedList;
public class _735 {
public static class Solution1 {
public int[] asteroidCollision(int[] asteroids) {
Deque<Integer> stack = new LinkedList<>();
for (int i = 0; i < asteroids.length; i++) {
if (!stack.isEmpty() && stack.peek() > 0 && asteroids[i] < 0) {
if (Math.abs(stack.peek()) < Math.abs(asteroids[i])) {
stack.pop();
stack.push(asteroids[i]);
collide(stack);
} else if (Math.abs(stack.peek()) == Math.abs(asteroids[i])) {
stack.pop();
}
} else {
stack.push(asteroids[i]);
}
}
int[] result = new int[stack.size()];
int i = stack.size();
while (!stack.isEmpty()) {
result[--i] = stack.pop();
}
return result;
}
private void collide(Deque<Integer> stack) {
do {
Integer top = stack.pop();
if (!stack.isEmpty() && stack.peek() * top < 0) {
if (stack.peek() < Math.abs(top)) {
stack.pop();
stack.push(top);
} else if (stack.peek() == Math.abs(top)) {
stack.pop();
break;
} else {
break;
}
} else if (stack.isEmpty() || stack.peek() * top > 0) {
stack.push(top);
break;
}
} while (!stack.isEmpty());
}
}
public static class Solution2 {
/*
* My completely original solution on 11/5/2021.
*/
public int[] asteroidCollision(int[] asteroids) {
Deque<Integer> stack = new LinkedList<>();
for (int a : asteroids) {
if (a > 0) {
stack.addLast(a);
} else {
if (!stack.isEmpty() && stack.peekLast() > 0) {
if (stack.peekLast() > Math.abs(a)) {
continue;
} else if (stack.peekLast() == Math.abs(a)) {
stack.pollLast();
} else {
while (!stack.isEmpty()
&& stack.peekLast() > 0
&& stack.peekLast() < Math.abs(a)) {
stack.pollLast();
}
if (!stack.isEmpty()
&& stack.peekLast() > 0
&& stack.peekLast() == Math.abs(a)) {
stack.pollLast();
continue;
} else if (stack.isEmpty() || stack.peekLast() < 0) {
stack.addLast(a);
}
}
} else {
stack.addLast(a);
}
}
}
int[] ans = new int[stack.size()];
for (int i = stack.size() - 1; i >= 0; i--) {
ans[i] = stack.pollLast();
}
return ans;
}
}
public static class Solution3 {
/*
* My completely original solution on 1/14/2022.
*/
public int[] asteroidCollision(int[] asteroids) {
Deque<Integer> stack = new LinkedList<>();
for (int i = 0; i < asteroids.length; i++) {
int a = asteroids[i];
if (a > 0) {
stack.addLast(a);
} else {
if (!stack.isEmpty() && stack.peekLast() > 0) {
if (stack.peekLast() > Math.abs(a)) {
continue;
} else if (stack.peekLast() == Math.abs(a)) {
stack.pollLast();
} else {
stack.pollLast();
i--;
}
} else {
stack.addLast(a);
}
}
}
int[] ans = new int[stack.size()];
for (int i = ans.length - 1; i >= 0; i--) {
ans[i] = stack.pollLast();
}
return ans;
}
}
public static class Solution4 {
/*
* My completely original solution on 7/19/2024.
*/
public int[] asteroidCollision(int[] asteroids) {
Deque<Integer> stack = new LinkedList<>();
for (int asteroid : asteroids) {
if (asteroid < 0 && !stack.isEmpty() && stack.peekLast() > 0) {
boolean bothRemoved = false;
while (!stack.isEmpty()
&& stack.peekLast() > 0
&& stack.peekLast() <= -asteroid) {
if (stack.peekLast() == -asteroid) {
bothRemoved = true;
stack.pollLast();
break;
} else if (stack.peekLast() < -asteroid) {
stack.pollLast();
}
}
if ((stack.isEmpty() || stack.peekLast() < 0) && !bothRemoved) {
stack.addLast(asteroid);
}
} else {
stack.addLast(asteroid);
}
}
int[] result = new int[stack.size()];
int i = 0;
while (!stack.isEmpty()) {
result[i++] = stack.pollFirst();
}
return result;
}
}
}