当前位置:网站首页>[735. Planetary collision]
[735. Planetary collision]
2022-07-28 04:02:00 【[email protected]】
source : Power button (LeetCode)
describe :
Given an array of integers asteroids, Represents planets on the same line .
For each element in the array , Its absolute value represents the size of the planet , Positive and negative indicate the direction of movement of the planet ( Positive means moving to the right , Negative means moving to the left ). Every planet moves at the same speed .
Find all the planets left after the collision . Collision rules : The two planets collide with each other , Smaller planets will explode . If two planets are the same size , Then both planets will explode . Two planets moving in the same direction , There will never be a collision .
Example 1:
Input :asteroids = [5,10,-5]
Output :[5,10]
explain :10 and -5 After the collision, only 10 . 5 and 10 There will never be a collision .
Example 2:
Input :asteroids = [8,-8]
Output :[]
explain :8 and -8 After the collision , Both exploded .
Example 3:
Input :asteroids = [10,2,-5]
Output :[10]
explain :2 and -5 After the collision, there are -5 .10 and -5 After the collision, there are 10 .
Tips :
- 2 <= asteroids.length <= 104
- -1000 <= asteroids[i] <= 1000
- asteroids[i] != 0
Method : Stack simulation
Use stack st Simulate planetary collision , Traverse the planetary array from left to right asteroids, When we traverse the planet aster when , Using variables alive Record planets aster Does it still exist ( That is, unexploded ).
When the planet aster Exist and aster < 0, The stack top element is not empty and greater than 0 when , It shows that the two planets move towards each other : If the top element is greater than or equal to −aster, Then the planet aster Explosion , take alive Set as false; If the stack top element is less than or equal to −aster, Then the planet represented by the top element of the stack explodes , Perform stack out operation . Repeat the above judgment until the conditions are not met , If the last alive It's true , Explain the planet aster It doesn't explode , Will aster Push .
Code :
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
vector<int> st;
for (auto aster : asteroids) {
bool alive = true;
while (alive && aster < 0 && !st.empty() && st.back() > 0) {
alive = st.back() < -aster; // aster Whether there is
if (st.back() <= -aster) {
// The top planet exploded
st.pop_back();
}
}
if (alive) {
st.push_back(aster);
}
}
return st;
}
};
Execution time :4 ms, In all C++ Defeated in submission 99.37% Users of
Memory consumption :17 MB, In all C++ Defeated in submission 72.79% Users of
Complexity analysis
Time complexity : O(n), among n Is an array asteroids Size . No more than n Time .
Spatial complexity : O(1). The return value is not included in the space complexity .
author:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/197/202207132046399775.html
边栏推荐
- "Three no's and five requirements" principle of enterprise Digitalization Construction
- Detailed explanation of pointer written test questions (C language)
- 7/27(板子)染色法判定二分图+求组合数(递推公式)
- Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (first 8 questions)
- 40: Chapter 4: Development File Service: 1:fastdfs: (1): introduction to fastdfs;
- Dynamic planning - 62. Different paths
- Protocols in swift
- Un7.27: how to successfully build a ruoyi framework project in idea?
- Greedy - 53. Maximum subarray sum
- 【伸手党福利】微信中h5网页调起扫一扫最简单的方法
猜你喜欢
![[prototype and prototype chain] get to know prototype and prototype chain~](/img/8a/d6362fdd50dc883ff817a997ab9e1e.png)
[prototype and prototype chain] get to know prototype and prototype chain~

numeric_ Limits the range and related attributes of each data type learned

Data rich Computing: m.2 meets AI at the edge

MySQL是怎么保证高可用的

基于SSM实现在线租房系统

Experience sharing of automatic test for students with monthly salary of 28K

Lightpicture - exquisite drawing bed system

Fourier series

Analysis of static broadcast transmission process

Selenium--WEB自动化测试工具
随机推荐
Dynamic programming - 474. One and zero
Appnium -- app automated test tool
Super easy to use PC end long screenshot tool
离职前一定要做好这7件事情,少一件都很麻烦。
numeric_ Limits the range and related attributes of each data type learned
Move notice!
Combination of Oracle and Premier League statistics and presentation
Greed 45. Jumping game II
基于SSM实现在线租房系统
【伸手党福利】微信中h5网页调起扫一扫最简单的方法
40: Chapter 4: Development File Service: 1:fastdfs: (1): introduction to fastdfs;
Input upload file and echo FileReader and restrict the type of file selection
Analysis of static broadcast transmission process
servlet使用
security异常处理机制
ServletContext、request、response
Cookies and session
Filters, interceptors, listeners
Data rich Computing: m.2 meets AI at the edge
Classification cluster analysis