forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_735.java
123 lines (118 loc) · 4.45 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
package com.fishercoder.solutions;
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;
}
}
}