← 返回 pinterest 的题目列表Find the Nearest Eligible Elevator
类型:online_judge
Problem: Find the Nearest Eligible Elevator
You are given n elevators. Elevator i has:
a current floor floors[i]
a current state states[i], which is one of "up", "down", or "idle"
You are also given one passenger with:
a current floor passenger_floor
a desired direction passenger_direction, which is either "up" or "down"
Return the index of the nearest elevator that can pick up the passenger. Return -1 if no elevator is eligible.
Eligibility Rules
An idle elevator can pick up any passenger.
An elevator moving up can pick up only a passenger who:
wants to go up, and
is on the elevator's current floor or above it.
An elevator moving down can pick up only a passenger who:
wants to go down, and
is on the elevator's current floor or below it.
If an elevator and the passenger are on the same floor but move in opposite directions, that elevator cannot pick up the passenger.
If multiple eligible elevators have the same minimum distance, return the smallest index.
The distance between an elevator and the passenger is abs(floors[i] - passenger_floor).
Input Format
n
floors[0] floors[1] ... floors[n-1]
states[0] states[1] ... states[n-1]
passenger_floor passenger_direction
Output Format
The index of the nearest eligible elevator, or -1 if none exists.
Constraints
1 <= n <= 2 * 10^5
0 <= floors[i], passenger_floor <= 10^9
states[i] is one of up, down, or idle
passenger_direction is either up or down
Example 1
Input:
3
2 8 12
up down idle
10 up
Output:
2
Example 2
Input:
2
5 5
up down
5 up
Output:
0
Example
Input
3
2 8 12
up down idle
10 up
Output
2