Skip to content

Commit e338d1d

Browse files
add a solution for 735
1 parent b82c7f8 commit e338d1d

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/main/java/com/fishercoder/solutions/_735.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.Stack;
3+
import java.util.Deque;
4+
import java.util.LinkedList;
45

56
public class _735 {
67
public static class Solution1 {
78
public int[] asteroidCollision(int[] asteroids) {
8-
Stack<Integer> stack = new Stack();
9+
Deque<Integer> stack = new LinkedList<>();
910
for (int i = 0; i < asteroids.length; i++) {
1011
if (!stack.isEmpty() && stack.peek() > 0 && asteroids[i] < 0) {
1112
if (Math.abs(stack.peek()) < Math.abs(asteroids[i])) {
@@ -27,7 +28,7 @@ public int[] asteroidCollision(int[] asteroids) {
2728
return result;
2829
}
2930

30-
private void collide(Stack<Integer> stack) {
31+
private void collide(Deque<Integer> stack) {
3132
do {
3233
Integer top = stack.pop();
3334
if (!stack.isEmpty() && stack.peek() * top < 0) {
@@ -47,4 +48,43 @@ private void collide(Stack<Integer> stack) {
4748
} while (!stack.isEmpty());
4849
}
4950
}
51+
52+
public static class Solution2 {
53+
/**
54+
* My completely original solution on 11/5/2021.
55+
*/
56+
public int[] asteroidCollision(int[] asteroids) {
57+
Deque<Integer> stack = new LinkedList<>();
58+
for (int a : asteroids) {
59+
if (a > 0) {
60+
stack.addLast(a);
61+
} else {
62+
if (!stack.isEmpty() && stack.peekLast() > 0) {
63+
if (stack.peekLast() > Math.abs(a)) {
64+
continue;
65+
} else if (stack.peekLast() == Math.abs(a)) {
66+
stack.pollLast();
67+
} else {
68+
while (!stack.isEmpty() && stack.peekLast() > 0 && stack.peekLast() < Math.abs(a)) {
69+
stack.pollLast();
70+
}
71+
if (!stack.isEmpty() && stack.peekLast() > 0 && stack.peekLast() == Math.abs(a)) {
72+
stack.pollLast();
73+
continue;
74+
} else if (stack.isEmpty() || stack.peekLast() < 0) {
75+
stack.addLast(a);
76+
}
77+
}
78+
} else {
79+
stack.addLast(a);
80+
}
81+
}
82+
}
83+
int[] ans = new int[stack.size()];
84+
for (int i = stack.size() - 1; i >= 0; i--) {
85+
ans[i] = stack.pollLast();
86+
}
87+
return ans;
88+
}
89+
}
5090
}

src/test/java/com/fishercoder/_735Test.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88

99
public class _735Test {
1010
private static _735.Solution1 solution1;
11+
private static _735.Solution2 solution2;
1112
private static int[] asteroids;
13+
private static int[] expected;
1214

1315
@BeforeClass
1416
public static void setup() {
1517
solution1 = new _735.Solution1();
18+
solution2 = new _735.Solution2();
1619
}
1720

1821
@Test
1922
public void test1() {
2023
asteroids = new int[]{5, 10, -5};
21-
asteroids = solution1.asteroidCollision(asteroids);
22-
assertArrayEquals(new int[]{5, 10}, asteroids);
24+
expected = new int[]{5, 10};
25+
assertArrayEquals(expected, solution1.asteroidCollision(asteroids));
26+
assertArrayEquals(expected, solution2.asteroidCollision(asteroids));
2327
}
2428

2529
@Test

0 commit comments

Comments
 (0)