Skip to content

Commit 44b0dcb

Browse files
committed
leetcode
1 parent 0d8577a commit 44b0dcb

File tree

4 files changed

+362
-0
lines changed

4 files changed

+362
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
3+
4+
-* 735. Asteroid Collision *-
5+
6+
We are given an array asteroids of integers representing asteroids in a row.
7+
8+
For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
9+
10+
Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
11+
12+
13+
14+
Example 1:
15+
16+
Input: asteroids = [5,10,-5]
17+
Output: [5,10]
18+
Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
19+
Example 2:
20+
21+
Input: asteroids = [8,-8]
22+
Output: []
23+
Explanation: The 8 and -8 collide exploding each other.
24+
Example 3:
25+
26+
Input: asteroids = [10,2,-5]
27+
Output: [10]
28+
Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
29+
30+
31+
Constraints:
32+
33+
2 <= asteroids.length <= 104
34+
-1000 <= asteroids[i] <= 1000
35+
asteroids[i] != 0
36+
37+
38+
39+
*/
40+
41+
// Iteration
42+
class A {
43+
List<int> asteroidCollision(List<int> asteroids) {
44+
final int n = asteroids.length;
45+
int j = 0;
46+
47+
for (int i = 0; i < n; i++) {
48+
final int asteroid = asteroids[i];
49+
while (j > 0 &&
50+
asteroids[j - 1] > 0 &&
51+
asteroid < 0 &&
52+
asteroids[j - 1] < asteroid.abs()) {
53+
j--;
54+
}
55+
56+
if (j == 0 || asteroid > 0 || asteroids[j - 1] < 0) {
57+
asteroids[j++] = asteroid;
58+
} else if (asteroids[j - 1] == asteroid.abs()) {
59+
j--;
60+
}
61+
}
62+
63+
final List<int> result = List<int>.from(asteroids.sublist(0, j));
64+
return result;
65+
}
66+
}
67+
68+
// Doubly Linked List
69+
70+
class ListNode {
71+
int val;
72+
bool pos;
73+
ListNode? next;
74+
ListNode? prev;
75+
76+
ListNode({this.next, this.prev, this.val = 0, this.pos = false});
77+
}
78+
79+
class Solution {
80+
List<int> asteroidCollision(List<int> a) {
81+
final ListNode head = ListNode(pos: false);
82+
ListNode current = head;
83+
84+
final int n = a.length;
85+
86+
for (int i = 0; i < n; ++i) {
87+
final ListNode node = ListNode(val: a[i].abs(), pos: a[i] > 0);
88+
current.next = node;
89+
node.prev = current;
90+
current = node;
91+
}
92+
93+
final ListNode tail = ListNode(pos: true);
94+
current.next = tail;
95+
tail.prev = current;
96+
97+
current = head;
98+
while (current != tail) {
99+
if (current.pos) {
100+
ListNode nextNode = current.next!;
101+
if (nextNode.pos) {
102+
current = nextNode;
103+
} else {
104+
if (nextNode.val == current.val) {
105+
final ListNode prevNode = current.prev!;
106+
// var delete1 = current;
107+
final ListNode delete2 = current.next!;
108+
nextNode = delete2.next!;
109+
prevNode.next = nextNode;
110+
nextNode.prev = prevNode;
111+
current = prevNode;
112+
} else if (nextNode.val > current.val) {
113+
final ListNode prevNode = current.prev!;
114+
prevNode.next = nextNode;
115+
nextNode.prev = prevNode;
116+
// var toDelete = current;
117+
if (prevNode.pos)
118+
current = current.prev!;
119+
else
120+
current = current.next!;
121+
} else {
122+
// var toDelete = nextNode;
123+
nextNode = nextNode.next!;
124+
current.next = nextNode;
125+
nextNode.prev = current;
126+
}
127+
}
128+
} else {
129+
current = current.next!;
130+
}
131+
}
132+
133+
final List<int> ans = [];
134+
current = head.next!;
135+
while (current != tail) {
136+
final int value = current.pos ? current.val : -current.val;
137+
ans.add(value);
138+
current = current.next!;
139+
}
140+
return ans;
141+
}
142+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package main
2+
3+
import "math"
4+
5+
/*
6+
type LinkNode struct {
7+
val int
8+
pos bool
9+
next *LinkNode
10+
prev *LinkNode
11+
}
12+
13+
func asteroidCollision(a []int) []int {
14+
head := &LinkNode{pos: false}
15+
current := head
16+
17+
n := len(a)
18+
19+
for i := 0; i < n; i++ {
20+
node := &LinkNode{val: int(math.Abs(float64(a[i]))), pos: a[i] > 0}
21+
current.next = node
22+
node.prev = current
23+
current = node
24+
}
25+
26+
tail := &LinkNode{pos: true}
27+
current.next = tail
28+
tail.prev = current
29+
30+
current = head
31+
for current != tail {
32+
if current.pos {
33+
nextNode := current.next
34+
if nextNode.pos {
35+
current = nextNode
36+
} else {
37+
if nextNode.val == current.val {
38+
prevNode := current.prev
39+
delete1 := current
40+
delete2 := current.next
41+
nextNode = delete2.next
42+
prevNode.next = nextNode
43+
if nextNode != nil {
44+
nextNode.prev = prevNode
45+
}
46+
current = prevNode
47+
_ = delete1
48+
_ = delete2
49+
} else if nextNode.val > current.val {
50+
prevNode := current.prev
51+
prevNode.next = nextNode
52+
if nextNode != nil {
53+
nextNode.prev = prevNode
54+
}
55+
if prevNode.pos {
56+
current = current.prev
57+
} else {
58+
current = current.next
59+
}
60+
} else {
61+
_ = nextNode
62+
nextNode = nextNode.next
63+
current.next = nextNode
64+
if nextNode != nil {
65+
nextNode.prev = current
66+
}
67+
}
68+
}
69+
} else {
70+
current = current.next
71+
}
72+
}
73+
74+
var ans []int
75+
current = head.next
76+
for current != tail {
77+
value := current.val
78+
if !current.pos {
79+
value = -value
80+
}
81+
ans = append(ans, value)
82+
current = current.next
83+
}
84+
85+
return ans
86+
}
87+
*/
88+
89+
func asteroidCollision(asteroids []int) []int {
90+
n := len(asteroids)
91+
j := 0
92+
93+
for i := 0; i < n; i++ {
94+
asteroid := asteroids[i]
95+
for j > 0 && asteroids[j-1] > 0 && asteroid < 0 && asteroids[j-1] < int(math.Abs(float64(asteroid))) {
96+
j--
97+
}
98+
99+
if j == 0 || asteroid > 0 || asteroids[j-1] < 0 {
100+
asteroids[j] = asteroid
101+
j++
102+
} else if asteroids[j-1] == int(math.Abs(float64(asteroid))) {
103+
j--
104+
}
105+
}
106+
107+
result := make([]int, j)
108+
copy(result, asteroids[:j])
109+
return result
110+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#
2+
3+
## 1
4+
5+
```dart
6+
class Solution {
7+
List<int> asteroidCollision(List<int> asteroids) {
8+
final int n = asteroids.length;
9+
int j = 0;
10+
11+
for (int i = 0; i < n; i++) {
12+
final int asteroid = asteroids[i];
13+
while (j > 0 &&
14+
asteroids[j - 1] > 0 &&
15+
asteroid < 0 &&
16+
asteroids[j - 1] < asteroid.abs()) {
17+
j--;
18+
}
19+
20+
if (j == 0 || asteroid > 0 || asteroids[j - 1] < 0) {
21+
asteroids[j++] = asteroid;
22+
} else if (asteroids[j - 1] == asteroid.abs()) {
23+
j--;
24+
}
25+
}
26+
27+
final List<int> result = List<int>.from(asteroids.sublist(0, j));
28+
return result;
29+
}
30+
}
31+
```
32+
33+
## 2
34+
35+
```dart
36+
37+
class ListNode {
38+
int val;
39+
bool pos;
40+
ListNode? next;
41+
ListNode? prev;
42+
43+
ListNode({this.next, this.prev, this.val = 0, this.pos = false});
44+
}
45+
46+
47+
48+
class Solution {
49+
List<int> asteroidCollision(List<int> a) {
50+
final ListNode head = ListNode(pos: false);
51+
ListNode current = head;
52+
53+
final int n = a.length;
54+
55+
for (int i = 0; i < n; ++i) {
56+
final ListNode node = ListNode(val: a[i].abs(), pos: a[i] > 0);
57+
current.next = node;
58+
node.prev = current;
59+
current = node;
60+
}
61+
62+
final ListNode tail = ListNode(pos: true);
63+
current.next = tail;
64+
tail.prev = current;
65+
66+
current = head;
67+
while (current != tail) {
68+
if (current.pos) {
69+
ListNode nextNode = current.next!;
70+
if (nextNode.pos) {
71+
current = nextNode;
72+
} else {
73+
if (nextNode.val == current.val) {
74+
final ListNode prevNode = current.prev!;
75+
final ListNode delete2 = current.next!;
76+
nextNode = delete2.next!;
77+
prevNode.next = nextNode;
78+
nextNode.prev = prevNode;
79+
current = prevNode;
80+
} else if (nextNode.val > current.val) {
81+
final ListNode prevNode = current.prev!;
82+
prevNode.next = nextNode;
83+
nextNode.prev = prevNode;
84+
if (prevNode.pos)
85+
current = current.prev!;
86+
else
87+
current = current.next!;
88+
} else {
89+
nextNode = nextNode.next!;
90+
current.next = nextNode;
91+
nextNode.prev = current;
92+
}
93+
}
94+
} else {
95+
current = current.next!;
96+
}
97+
}
98+
99+
final List<int> ans = [];
100+
current = head.next!;
101+
while (current != tail) {
102+
final int value = current.pos ? current.val : -current.val;
103+
ans.add(value);
104+
current = current.next!;
105+
}
106+
return ans;
107+
}
108+
}
109+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
248248
- [**863.** All Nodes Distance K in Binary Tree](AllNodesDistanceKInBinaryTree)
249249
- [**207.** Course Schedule](CourseSchedule)
250250
- [**146.** LRU Cache](LRUCache)
251+
- [**735.** Asteroid Collision](AsteroidCollision)
251252

252253
## Reach me via
253254

0 commit comments

Comments
 (0)