FastPrepFastPrep
Problem Brief

Get Minimum Time

OA

The other question in this batch is LC 53. Maximum Subarray :)

Your office is relocating to the top floor of a building with n+1 floors, indexed from 0 to n. Currently, boxes of equipment and supplies are scattered across the lower floors (0 to n-1), and you need to transport all of them to the top floor (n).

Rules:

  • You start at the top floor (n).
  • You can take the elevator down to any floor i and then return to the top (n).
  • It takes (n - i) minutes to travel down to floor i and the same time to return.
  • At each stop, you can load a maximum of one box from that floor into the elevator.
  • Loading and unloading a box together take 1 minute.
  • Input:

  • An integer n (1 ≤ n ≤ 10^5) — the top floor index.
  • An array of n integers, where boxes[i] (0 ≤ boxes[i] ≤ 10^9) represents the number of boxes on floor i.
  • Output:

    A single long integer — the minimum time required to move all boxes to the top floor.

    1Example 1

    Input
    n = 3, boxes = [1, 2, 3]
    Output
    12
    Explanation
    Start at floor 3.
    • Go to floor 2 (1 minute), pick up a box, return to floor 3 (1 minute) → 2 minutes.
    • Go to floor 1 (2 minutes), pick up a box from 2 and 1, return (2 minutes) → 4 minutes.
    • Go to floor 0 (3 minutes), pick up all remaining boxes, return (3 minutes) → 6 minutes.
    • Total time: 2 + 4 + 6 = 12 minutes.
    public long getMinimumTime(int n, int[] boxes) {
      // write your code here
    }
    
    Input

    n

    3

    boxes

    [1, 2, 3]

    Output

    12

    Sign in to submit your solution.