Get Minimum Time
Updated
Problem statement
Your office is relocating to the top floor of a building with n + 1 floors, numbered 0 to n. Boxes of equipment and supplies are on the lower floors: floor i (for 0 ≤ i ≤ n - 1) has boxes[i] boxes. You need to move all of them to the top floor n.
Rules:
- You start at the top floor
n. - A trip takes the elevator down to some floor
iand then back up to the top floor. Going down takesn - iminutes, and coming back up takes anothern - iminutes. - The elevator can stop at any floor it passes. During one trip, you can load at most one box from each floor.
- Loading and unloading a box together take 1 minute. So every box adds 1 minute, whichever trip carries it.
Return the minimum total number of minutes needed to move every box to the top floor.
Function
getMinimumTime(n: int, boxes: int[]) → long Complete the function getMinimumTime in the editor.
getMinimumTime has the following parameters:
int n: the index of the top floorint[] boxes:boxes[i]is the number of boxes on floori
The function returns a long: the minimum time in minutes.
Examples
Example 1
n = 3boxes = [1, 2, 3]return = 18Floor 0 has 1 box, floor 1 has 2 boxes and floor 2 has 3 boxes. Floor 2 needs 3 trips, because each trip takes at most one box from it.
- Trip 1: down to floor 2 and back, 1 + 1 = 2 minutes. It carries 1 box from floor 2.
- Trip 2: down to floor 1 and back, 2 + 2 = 4 minutes. It carries 1 box from floor 1 and 1 box from floor 2.
- Trip 3: down to floor 0 and back, 3 + 3 = 6 minutes. It carries the last box from each of floors 0, 1 and 2.
Travel takes 2 + 4 + 6 = 12 minutes. Loading and unloading the 6 boxes adds 6 minutes, so the total is 12 + 6 = 18 minutes.
No plan travels less: some trip must reach floor 0 (6 minutes), a second trip must reach floor 1 (at least 4 minutes), and a third trip must reach floor 2 (at least 2 minutes).
Constraints
1 ≤ n ≤ 105boxes.length = n0 ≤ boxes[i] ≤ 109- The answer can be larger than
231 - 1, so it needs a 64-bit integer.
Similar questions at other companies
- Earn the Most 🐝Capital One · OACapital One interview questions
- Minimum Cost to Merge Values into OnePayPal · OAPayPal interview questions
- Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
- Minimum Cost of Left and Right PropagationAmazon · OA
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Reschedule Meetings for Maximum Free TimeFivetran · OAFivetran interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
- Maximum ScoreNVIDIA · OANVIDIA interview questions
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA