FastPrepGet Minimum Time
Problems/Get Minimum Time

Get Minimum Time

Publicis Sapient logoPublicis Sapient● MediumOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

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 i and then back up to the top floor. Going down takes n - i minutes, and coming back up takes another n - i minutes.
  • 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:

  1. int n: the index of the top floor
  2. int[] boxes: boxes[i] is the number of boxes on floor i

The function returns a long: the minimum time in minutes.

Examples

Example 1

n = 3boxes = [1, 2, 3]return = 18

Floor 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 ≤ 105
  • boxes.length = n
  • 0 ≤ 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
See Publicis Sapient hiring insights
Saved
class Solution {
    public long getMinimumTime(int n, int[] boxes) {
      // write your code here
    }

}
Checking account…