Problem Statement
A building is serviced by a single elevator. The elevator services requests to go up or down. If an elevator receives a request while it is not moving, it starts servicing the request immediately. If a request comes while the elevator is in motion, the elevator processes the new request immediately only if doing so does not require the elevator to change its direction. Otherwise, the elevator finishes servicing its current request before servicing the new request. Given the current position of the elevator, a destination to which the elevator is moving, and the source of a new request, calculate the number of floors the elevator needs to travel before it could start servicing the new request.
The position argument represents the elevator's current floor. The destination argument represents the destination floor to which the elevator is moving now. If the position equals the destination, the elevator is not moving.
The request argument represents the floor on which the new request originates. The direction argument indicates whether the request is to go up (+1) or down (-1). For example, an input combination of position=4, destination=10, request=5, and direction=-1 describes a situation when the elevator is passing the fourth floor on its way to the tenth floor, while the request to go down is coming from the fifth floor.
Your method should return the number of floors the elevator needs to travel before it could start servicing the new request. Assume that the elevator has no other requests to service.
Definition
- Class:
- ElevatorMove
- Method:
- distance
- Parameters:
- int, int, int, int
- Returns:
- int
- Method signature:
- int distance(int position, int destination, int request, int direction)
- (be sure your method is public)
Constraints
- position, destination, and request are numbers between 1 and 12, inclusive.
- direction is either 1 indicating a request to go up, or -1 indicating a request to go down.
- The direction of a request coming from the top floor is always -1.
- The direction of a request coming from the first floor is always 1.
Examples
4
10
5
1
Returns: 1
The elevator is on its way up, and it is below the floor from which the new request to go up originated. The elevator can start servicing the new request right away, because doing so does not require a change of direction. The elevator has a single floor to travel from 4 to 5.4
10
5
-1
Returns: 11
The elevator is on its way up, but the new request is to go down. The elevator must finish with its current request before proceeding to the new one. It needs to travel 6 floors to its current destination, and then 5 more floors to get back to the origin of the new request.4
4
4
-1
Returns: 0
The elevator does not need to move because it is already on the correct floor.4
10
4
-1
Returns: 12
Although the elevator is on the correct floor, it is moving up, not down. The elevator must complete its current trip before servicing the new request.4
10
3
1
Returns: 13
The elevator is moving in the direction of the request, but it has passed the floor from which the request originated. The elevator must complete its current trip, and come back.4
10
11
-1
Returns: 7
The elevator needs to pass its current destination (10) on the way to the floor from which the new request originated (11), so it needs to travel six floors.