Elevator Simulation
Function Name: elevatorDispatch
Description
Write a function that simulates the basic operation of an elevator dispatch system. The function's goal is to determine which elevator should be sent to a requested floor based on the current status of each elevator and a series of pick-up requests.
Each elevator's status and the pick-up requests are defined by two parameters: a list of elevator objects with their current floor and direction of movement ('up', 'down', or 'idle'), and a list of pick-up requests as pairs (tuples) of integers representing the requested floor and desired direction ('1' for up and '-1' for down).
The function must select the most appropriate elevator for each request based on proximity and direction. If multiple elevators are equally suitable, prioritize the one with the lower index in the elevator list.
Requirements
- The function should take two parameters: a list of elevator statuses and a list of pick-up requests.
- Return a list of integers, where each integer represents the index of the elevator chosen for the corresponding pick-up request.
- Elevators should continue moving in their current direction if they have pending stops in that direction.
- If an elevator is 'idle', it can immediately move in either direction.
- Once an elevator has no more pending stops in its current direction, it should become 'idle' and be available for new requests in any direction.
Examples
Given 3 elevators with statuses: [{floor: 2, direction: 'up'}, {floor: 5, direction: 'down'}, {floor: 1, direction: 'idle'}]And pick-up requests: [(3, 1), (1, -1), (4, -1)]elevatorDispatch(elevatorStatuses, pickUpRequests) should return [0, 2, 1] meaning the first request is handled by Elevator 0, the second by Elevator 2, and the third by Elevator 1.
Matrix Spiral Copy
Fri Nov 22 2024
Write a function that receives a two-dimensional array (matrix) of integers and returns a list of the matrix's elements in spiral order. Spiral order should start from the top-left corner and progress clockwise until all elements have been visited.
Merge Intervals
Sun Nov 24 2024
You are given an array of 'intervals' where intervals[i] = [starti, endi] represents the start and end of an exclusive time interval. Write a function to merge all overlapping intervals into a single one and return it in a way that minimizes the number of intervals. The function should return an array of the merged intervals sorted in ascending order by the start time.Two intervals [a, b] and [c, d] overlap if b > c, assuming a < b and c < d. The merge should result in a new interval [a, d] if a < c and b > c (or vice versa).Your function should be able to handle an arbitrary number of intervals and should consider edge cases where intervals are contained within one another or when there are no overlapping intervals.