加载中...
735-行星碰撞(Asteroid Collision)
发表于:2021-12-03 | 分类: 中等
字数统计: 536 | 阅读时长: 2分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/asteroid-collision

英文原文

We are given an array asteroids of integers representing asteroids in a row.

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.

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.

 

Example 1:

Input: asteroids = [5,10,-5]
Output: [5,10]
Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.

Example 2:

Input: asteroids = [8,-8]
Output: []
Explanation: The 8 and -8 collide exploding each other.

Example 3:

Input: asteroids = [10,2,-5]
Output: [10]
Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.

Example 4:

Input: asteroids = [-2,-1,1,2]
Output: [-2,-1,1,2]
Explanation: The -2 and -1 are moving left, while the 1 and 2 are moving right. Asteroids moving the same direction never meet, so no asteroids will meet each other.

 

Constraints:

  • 2 <= asteroids.length <= 104
  • -1000 <= asteroids[i] <= 1000
  • asteroids[i] != 0

中文题目

给定一个整数数组 asteroids,表示在同一行的行星。

对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动)。每一颗行星以相同的速度移动。

找出碰撞后剩下的所有行星。碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。

 

示例 1:

输入:asteroids = [5,10,-5]
输出:[5,10]
解释:10 和 -5 碰撞后只剩下 10 。 5 和 10 永远不会发生碰撞。

示例 2:

输入:asteroids = [8,-8]
输出:[]
解释:8 和 -8 碰撞后,两者都发生爆炸。

示例 3:

输入:asteroids = [10,2,-5]
输出:[10]
解释:2 和 -5 发生碰撞后剩下 -5 。10 和 -5 发生碰撞后剩下 10 。

示例 4:

输入:asteroids = [-2,-1,1,2]
输出:[-2,-1,1,2]
解释-2 和 -1 向左移动,而 1 和 2 向右移动。 由于移动方向相同的行星不会发生碰撞,所以最终没有行星发生碰撞。 

 

提示:

  • 2 <= asteroids.length <= 104
  • -1000 <= asteroids[i] <= 1000
  • asteroids[i] != 0

通过代码

官方题解

方法:栈

如果不会发生碰撞那么一排小行星是处于稳定的状态。若在右边增加一个新的小行星后,在它再次稳定之前,可能会发生更多的碰撞,而所有的这些碰撞(如果发生的话)都必须从右到左发生。这种情况非常适合用栈解决。

算法:

  • 假设栈中顶部元素为 top,一个新的小行星 new 进来了。如果 new 向右移动(new>0),或者 top 向左移动(top<0),则不会发生碰撞。
  • 否则,如果 abs(new) < abs(top),则新小行星 new 将爆炸;如果 abs(new) == abs(top),则两个小行星都将爆炸;如果 abs(new) > abs(top),则 top 小行星将爆炸(可能还会有更多小行星爆炸,因此我们应继续检查)。
class Solution(object):
    def asteroidCollision(self, asteroids):
        ans = []
        for new in asteroids:
            while ans and new < 0 < ans[-1]:
                if ans[-1] < -new:
                    ans.pop()
                    continue
                elif ans[-1] == -new:
                    ans.pop()
                break
            else:
                ans.append(new)
        return ans
[ ]
class Solution { public int[] asteroidCollision(int[] asteroids) { Stack<Integer> stack = new Stack(); for (int ast: asteroids) { collision: { while (!stack.isEmpty() && ast < 0 && 0 < stack.peek()) { if (stack.peek() < -ast) { stack.pop(); continue; } else if (stack.peek() == -ast) { stack.pop(); } break collision; } stack.push(ast); } } int[] ans = new int[stack.size()]; for (int t = ans.length - 1; t >= 0; --t) { ans[t] = stack.pop(); } return ans; } }

复杂度分析

  • 时间复杂度:$O(N)$,其中 $N$ 是行星的数量。
  • 空间复杂度:$O(N)$,ans 使用的空间。

统计信息

通过次数 提交次数 AC比率
23160 56577 40.9%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言

相似题目

题目 难度
种花问题 简单
上一篇:
736-Lisp 语法解析(Parse Lisp Expression)
下一篇:
726-原子的数量(Number of Atoms)
本文目录
本文目录