Minimum Number of Seconds to Make Mountain Height Zero

You are given an integer mountainHeight denoting the height of a mountain.

You are also given an integer array workerTimes representing the work time of workers in seconds.

Each worker may reduce the mountain's height by any non-negative integer amount. If worker i reduces the height by x, then:

  • Reducing the first unit of height takes workerTimes[i] seconds.
  • Reducing the second unit takes workerTimes[i] * 2 seconds.
  • ...
  • Reducing the x-th unit takes workerTimes[i] * x seconds.

The total time spent by worker i is the sum of the times required for all x units they reduce. As all workers operate simultaneously, the total time required is the maximum time spent by any worker.

Return an integer representing the minimum number of seconds required for the workers to make the height of the mountain 0.

Example 1
InputmountainHeight = 4, workerTimes = [2,1,1]
Output3
One optimal assignment has workers reduce 1, 2, and 1 units respectively, taking max(2, 3, 1) = 3 seconds.
Example 2
InputmountainHeight = 10, workerTimes = [3,2,2,4]
Output12
Workers can reduce 2, 3, 3, and 2 units respectively, taking max(9, 12, 12, 12) = 12 seconds.

Constraints

  • 1 <= mountainHeight <= 10^5
  • 1 <= workerTimes.length <= 10^4
  • 1 <= workerTimes[i] <= 10^6

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate