Similar Problems

Similar Problems not available

Last Moment Before All Ants Fall Out Of A Plank - Leetcode Solution

Companies:

LeetCode:  Last Moment Before All Ants Fall Out Of A Plank Leetcode Solution

Difficulty: Medium

Topics: array simulation  

Problem:

There are n ants on a plank of length L at positions [0, L]. Each ant moves with a speed of 1 unit per second either left or right. When two ants meet at a position, they change direction and continue moving in the new direction. When an ant reaches the end of the plank, it falls off. Given an initial configuration of ants, find the last moment before all ants fall off the plank.

Solution:

We can solve the problem by simulating the movement of the ants on the plank. We can represent the position of an ant using an integer variable, where positions to the left of the origin (i.e. negative positions) represent ants moving left, and positions to the right (i.e. positive positions) represent ants moving right.

We can initialize an array of positions for the ants with their initial positions on the plank. We can then simulate the movement of the ants by updating their positions at every time step. At every time step, we check if two ants are about to collide by comparing their positions. If two ants are about to collide, we swap their directions of movement. If an ant reaches the end of the plank, we remove it from the array of ants.

We can continue simulating the movement of the ants until there is only one ant left on the plank. The time at which the last ant falls off the plank is the answer to the problem.

Code:

Here's the code to solve the problem:

def getLastMoment(n: int, left: List[int], right: List[int]) -> int:
    # Initialize the array of ant positions
    ants = left + [l + r for l, r in zip(left, right)]
    
    # Simulate the movement of the ants
    while len(ants) > 1:
        # Update the position of each ant
        ants = [ant + (1 if ant in left else -1) for ant in ants]
        
        # Check if two ants are about to collide
        for i in range(len(ants) - 1):
            if ants[i] < ants[i + 1]:
                ants[i], ants[i + 1] = ants[i + 1], ants[i]
        
        # Remove ants that have fallen off the plank
        ants = [ant for ant in ants if 0 <= ant <= n]
    
    # Return the time at which the last ant falls off the plank
    return max(ants) if ants else 0

The function getLastMoment takes the number of ants n, and the initial positions of the ants moving left and right left and right as input, and returns the time at which the last ant falls off the plank.

The function initializes the array of ant positions with the initial positions of the ants. It then simulates the movement of the ants by updating their positions at every time step. It checks if two ants are about to collide, and swaps their directions of movement if necessary. It removes ants that have fallen off the plank from the array. It continues simulating the movement of the ants until there is only one ant left on the plank.

The function returns the time at which the last ant falls off the plank if there is at least one ant left on the plank, and 0 otherwise.

Last Moment Before All Ants Fall Out Of A Plank Solution Code

1