LeetCode //C - 735. Asteroid Collision

735. 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.

Constraints:
  • 2 < = a s t e r o i d s . l e n g t h < = 1 0 4 2 <= asteroids.length <= 10^4 2<=asteroids.length<=104
  • -1000 <= asteroids[i] <= 1000
  • asteroids[i] != 0

From: LeetCode
Link: 735. Asteroid Collision


Solution:

Ideas:

To solve the “Asteroid Collision” problem in C, we need to implement the asteroidCollision function. This function takes an array of integers representing the asteroids, along with their size, and returns the state of the asteroids after all collisions have been resolved.

The key idea here is to use a stack to keep track of the asteroids. We iterate through the given array of asteroids and for each asteroid, we check for possible collisions. If an asteroid is moving to the right (positive integer), we push it onto the stack. If it’s moving to the left (negative integer), we check for collisions with the asteroids currently on the stack (which are moving to the right). We pop asteroids from the stack if they are smaller than the current left-moving asteroid. If we encounter an asteroid of the same size but in the opposite direction, both asteroids explode and we don’t add the current asteroid to the stack.

Code:
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* asteroidCollision(int* asteroids, int asteroidsSize, int* returnSize) {
    int* stack = (int*)malloc(asteroidsSize * sizeof(int));
    int top = -1;

    for (int i = 0; i < asteroidsSize; i++) {
        // If asteroid is moving right or stack is empty, push to stack
        if (asteroids[i] > 0 || top == -1) {
            stack[++top] = asteroids[i];
        } else {
            while (top != -1 && stack[top] > 0 && stack[top] < -asteroids[i]) {
                top--; // Pop smaller right-moving asteroids
            }
            if (top != -1 && stack[top] == -asteroids[i]) {
                top--; // Both asteroids explode
            } else if (top == -1 || stack[top] < 0) {
                stack[++top] = asteroids[i]; // Add left-moving asteroid
            }
            // If stack[top] > -asteroids[i], do nothing (right-moving asteroid survives)
        }
    }

    *returnSize = top + 1;
    return stack;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值