Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f1f5079

Browse files
✨ Day 21; Week 3 ✔️
1 parent f4a5008 commit f1f5079

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
6. [Sort List](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/560/week-2-october-8th-october-14th/3493/) ➡️ [CPP Solution](Week2/sortList.cpp)
2525
7. [House Robber II](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/560/week-2-october-8th-october-14th/3494/) ➡️ [CPP Solution](Week2/rob.cpp)
2626

27-
## Week 3 🚧
27+
## Week 3
2828

2929
1. [Rotate Array](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3496/) ➡️ [CPP Solution](Week3/rotate.cpp)
3030
2. [Search a 2D Matrix](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3497/) ➡️ [CPP Solution](Week3/searchMatrix.cpp)
3131
3. [Repeated DNA Sequences](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3498/) ➡️ [CPP Solution](Week3/findRepeatedDnaSequences.cpp)
3232
4. [Best Time to Buy and Sell Stock IV](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3499/) ➡️ [CPP Solution](Week3/maxProfit.cpp)
3333
5. [Minimum Domino Rotations For Equal Row](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3500/) ➡️ [CPP Solution](Week3/minDominoRotations.cpp)
3434
6. [Clone Graph](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3501/) ➡️ [CPP Solution](Week3/cloneGraph.cpp)
35+
7. [Asteroid Collision](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3502/) ➡️ [CPP Solution](Week3/asteroidCollision.cpp)
3536

3637

3738
## Week 4 🚧

‎Week3/asteroidCollision.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
vector<int> asteroidCollision(vector<int>& asteroids) {
4+
stack<int> s;
5+
6+
for(int x: asteroids) {
7+
if(x > 0) {
8+
s.push(x);
9+
} else {
10+
while(!s.empty() && s.top() > 0 && s.top() < abs(x))
11+
s.pop();
12+
13+
if(s.empty() || s.top() < 0)
14+
s.push(x);
15+
else if(s.top() == abs(x))
16+
s.pop();
17+
}
18+
}
19+
20+
vector<int> ans;
21+
22+
while(!s.empty()) {
23+
ans.push_back(s.top());
24+
s.pop();
25+
}
26+
27+
reverse(ans.begin(), ans.end());
28+
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /