Problem Brief

Collision

OA

There are n Toy cars moving in road and you are given array direction of size n where direction[i] = 1 or -1, 1 means car going to right and -1 means car going to left and array strength. If two car collide at any point the car with lower strength will Blast and if both cars strength are equal then both will Blast.

Returns how many cars will stay after all possible collision.

Function Description

CarsLeft has the following parameters:

  1. int n: The numbers of cars.
  2. int[] direction: The directions of each car.
  3. int[] strength: The strength of each car.

Returns

int[]: array Lists of Cars left after collisions in non-decreasing order.

1Example 1

Input
n = 3, direction = [1, -1, 1], strength = [5, 3, 2]
Output
[0, 2]
Explanation

Car 0 going right and car 1 going left strength[0] > strength[1]. Therefore, when 0 and 1 meet, 1 will Blast.

2Example 2

Input
n = 5, direction = [1, -1, -1, 1, -1, 1], strength = [7, 2, 3, 5, 5, 6]
Output
[0, 4]
Explanation

Both car 2 and 3 will blast when they collide.

Constraints

Limits and guarantees your solution can rely on.

  • n <= 2 * 10^5
  • direction[i] = 1 or direction[i] = -1
  • strength[i] <= 10^9
public int[] carsLeft(int n, int[] direction, int[] strength) {
  // write your code here
}
Input

n

3

direction

[1, -1, 1]

strength

[5, 3, 2]

Output

[0, 2]

Sign in to submit your solution.