Tap here to switch tabs
Problem
Submissions

Problem: Elevator Requests II

Elevator Requests II asks you to move from a starting floor to every distinct requested floor. Return the minimum sum of their first-arrival times, counting a request at the start as zero.

Statement

An elevator is currently at floor start in a building with n floors, numbered from 00 to n −1- 1.

You are given an integer n, an integer start, and an integer array requests containing distinct floor numbers that must be visited.

The elevator starts at time 00 at floor start and requires ∣a−b∣|a - b| seconds to move between floor aa and floor bb. The penalty for fulfilling a request is defined as the total time elapsed from time 00 until the elevator first arrives at that requested floor. If start is present in requests, its penalty is 00.

Return the minimum total penalty required to fulfill all requests in requests.

Constraints:

  • 1≤1 \leq n ≤109\leq 10^9

  • 1≤1 \leq requests.length ≤1500\leq 1500

  • 0≤0 \leq start, requests[i] ≤\leq n −1- 1

  • All values in requests are distinct.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Elevator Requests II

Elevator Requests II asks you to move from a starting floor to every distinct requested floor. Return the minimum sum of their first-arrival times, counting a request at the start as zero.

Statement

An elevator is currently at floor start in a building with n floors, numbered from 00 to n −1- 1.

You are given an integer n, an integer start, and an integer array requests containing distinct floor numbers that must be visited.

The elevator starts at time 00 at floor start and requires ∣a−b∣|a - b| seconds to move between floor aa and floor bb. The penalty for fulfilling a request is defined as the total time elapsed from time 00 until the elevator first arrives at that requested floor. If start is present in requests, its penalty is 00.

Return the minimum total penalty required to fulfill all requests in requests.

Constraints:

  • 1≤1 \leq n ≤109\leq 10^9

  • 1≤1 \leq requests.length ≤1500\leq 1500

  • 0≤0 \leq start, requests[i] ≤\leq n −1- 1

  • All values in requests are distinct.