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:
n).i and then return to the top (n).n - i) minutes to travel down to floor i and the same time to return.Input:
n (1 ≤ n ≤ 10^5) — the top floor index.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.