当前位置:网站首页>【LeetCode】735. Planetary collision
【LeetCode】735. Planetary collision
2022-07-19 03:45:00 【pass night】
subject
Medium difficulty 342 Switch to English to receive dynamic feedback
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] <= 1000asteroids[i] != 0
Ideas
- Use stack to simulate collision process
- Traverse the array from left to right , New planets constantly collide with the planets in the stack to the right until they die or collide with all the planets in the stack
Code
from collections import deque
class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack = deque()
for planet in asteroids:
alive = True
while alive and stack and stack[-1]>0 and planet<0:
alive = stack[-1] < -planet
if stack[-1] <= -planet: stack.pop()
if alive: stack.append(planet)
return list(stack)
Complexity
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( n ) O(n) O(n)
边栏推荐
- 未知宽高元素实现水平垂直居中的方法
- Fisher linear discriminant analysis
- 第二章:新闻主题分类任务
- Machine learning library scikit learn (linear model, ridge regression, insert a column of data, extract the required column, vector machine (SVM), clustering)
- ResNet
- Unity solves the problem of Z-fighting caused by overlapping objects with the same material
- Web semantics (emphasis tag EM italic) (emphasis tag strong bold) (custom list: DL, DT, DD)
- QT OpenGL moves 3D objects with mouse and keyboard (set camera)
- Reptile learning (5): teach you reptile requests practice hand in hand
- 电脑绘画软件哪个好用:试试Artweaver Plus吧,媲美sai绘画软件 | 最新版本的artweaver下载
猜你喜欢

论文阅读:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation

运算符、赋值语句、结构说明语句

kubernetes学习之持久化存储StorageClass(4)

渗透测试-02漏洞扫描

web语义化(强调标签-em-斜体)(重点强调标签-strong-粗体)(自定义列表:dl、dt、dd)

SparkCore核心设计:RDD,220716,
![[C language errata] error in getting array length in function](/img/3a/2de171251396ed1ffedf28ab221670.png)
[C language errata] error in getting array length in function

第二章 线性表

MYSQL主从搭建

論文閱讀:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
随机推荐
10. Redis interview FAQ
电脑绘画软件哪个好用:试试Artweaver Plus吧,媲美sai绘画软件 | 最新版本的artweaver下载
web语义化(强调标签-em-斜体)(重点强调标签-strong-粗体)(自定义列表:dl、dt、dd)
2.9.2 digital type processing and convenient methods of ext JS
Dive into deep learning - 2.2 data preprocessing
Reptile learning (5): teach you reptile requests practice hand in hand
Thinkphp5.0 model operation uses page for paging
二分查找(leetcode704.很简单必会的)
How to paste data in Excel into CXGRID
Web semantics (emphasis tag EM italic) (emphasis tag strong bold) (custom list: DL, DT, DD)
Analysis and processing of male and female voice signals based on MATLAB
ResNet
数学建模比赛论文模板格式
[C语言勘误]数组长度的函数内获取方式错误
oracle 查询非自增长分区的最大分区
Fisher linear discriminant analysis
Detailed explanation of arrow function and this direction
【C语言】0基础教程——文件操作(未完待续)
Binary search (leetcode704. very simple and necessary)
Matlab在线性代数中的应用