LeetCode-in-Java

4027. Elevator Requests III

Hard

You are given an integer n denoting the number of floors in a building, where the floors are numbered from 0 to n - 1.

You are also given an integer start and a 2D integer array requests, where requests[i] = [arrivali, floori] indicates that a request for floori is made at time arrivali.

At time 0, the elevator is at floor start.

At each second, the elevator may move up by 1 floor, move down by 1 floor, or remain on its current floor.

A request can be fulfilled only at or after its arrival time; it is fulfilled instantly when the elevator is on its requested floor at any time from its arrival time onward.

Return the minimum time needed to fulfill all requests.

Example 1:

Input: n = 9, start = 0, requests = [[0,8],[6,5]]

Output: 9

Explanation:

Thus, all requests are fulfilled by time 9.

Example 2:

Input: n = 8, start = 5, requests = [[1,7],[7,3]]

Output: 7

Explanation:

Thus, all requests are fulfilled by time 7.

Example 3:

Input: n = 7, start = 3, requests = [[0,5],[0,1],[6,3]]

Output: 8

Explanation:

Thus, all requests are fulfilled by time 8.

Constraints:

Solution

@SuppressWarnings("java:S1172")
public class Solution {
    public long elevatorRequests(int n, int start, int[][] requests) {
        return solve(start, 0L, 0, requests);
    }

    private long solve(int currFloor, long currTime, int fulfilledMask, int[][] requests) {
        int m = requests.length;
        if (fulfilledMask == (1 << m) - 1) {
            return currTime;
        }
        int minFloor = Integer.MAX_VALUE;
        int maxFloor = Integer.MIN_VALUE;
        for (int i = 0; i < m; i++) {
            if ((fulfilledMask & (1 << i)) == 0) {
                minFloor = Math.min(minFloor, requests[i][1]);
                maxFloor = Math.max(maxFloor, requests[i][1]);
            }
        }
        long res = Long.MAX_VALUE;
        int[] targets =
                (minFloor == maxFloor) ? new int[] {minFloor} : new int[] {minFloor, maxFloor};
        for (int target : targets) {
            int nextMask = fulfilledMask;
            long waitTargetTime = currTime + Math.abs(target - currFloor);
            for (int i = 0; i < m; i++) {
                if ((fulfilledMask & (1 << i)) == 0) {
                    int f = requests[i][1];
                    long arr = requests[i][0];
                    if ((currFloor <= f && f <= target) || (target <= f && f <= currFloor)) {
                        long passTime = currTime + Math.abs(f - currFloor);
                        if (passTime >= arr) {
                            nextMask |= (1 << i);
                        } else if (f == target) {
                            waitTargetTime = Math.max(waitTargetTime, arr);
                            nextMask |= (1 << i);
                        }
                    }
                }
            }
            res = Math.min(res, solve(target, waitTargetTime, nextMask, requests));
        }
        return res;
    }
}