Skip to the code
cf.
+10 XP

#573 Shortest Total Wait for Build Jobs

easy

A CI server has one runner and a queue of build jobs that all arrived at the same moment. Job i takes durations[i] minutes. Jobs run one after another, and each job waits from the start until it begins running.

Write smallestTotalWait(durations) that returns the smallest possible sum of every job's waiting time, if you may run the jobs in any order.

Example 1

Input: durations = [3,1,2]
Output: 4

Run 1, then 2, then 3: they wait 0, 1, and 3 minutes, for a total of 4.

Example 2

Input: durations = [5]
Output: 0

A single job starts right away.

Constraints

  • 0 <= durations.length <= 100000
  • 1 <= durations[i] <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).