Minimum Number of Seconds to Make Mountain Height Zero - Problem

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.

The workers work simultaneously to reduce the height of the mountain. For worker i:

  • To decrease the mountain's height by x, it takes workerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * x seconds.

For example:

  • To reduce the height of the mountain by 1, it takes workerTimes[i] seconds.
  • To reduce the height of the mountain by 2, it takes workerTimes[i] + workerTimes[i] * 2 seconds, and so on.

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

Input & Output

Example 1 — Basic Case
$ Input: mountainHeight = 4, workerTimes = [2,1,1]
Output: 3
💡 Note: Worker 1 reduces 1 unit (time: 2), Worker 2 reduces 2 units (time: 1+2=3), Worker 3 reduces 1 unit (time: 1). Maximum time is 3.
Example 2 — Single Worker
$ Input: mountainHeight = 3, workerTimes = [3]
Output: 18
💡 Note: Only worker reduces all 3 units: 3×1 + 3×2 + 3×3 = 3 + 6 + 9 = 18 seconds.
Example 3 — Equal Workers
$ Input: mountainHeight = 5, workerTimes = [1,1]
Output: 6
💡 Note: Optimal distribution: Worker 1 gets 3 units (1+2+3=6), Worker 2 gets 2 units (1+2=3). Maximum time is 6.

Constraints

  • 1 ≤ mountainHeight ≤ 105
  • 1 ≤ workerTimes.length ≤ 104
  • 1 ≤ workerTimes[i] ≤ 106

Visualization

Tap to expand
Mountain Reduction: Height=4, Workers=[2,1,1]Height: 4Worker 1Speed: 2Units: 1Worker 2Speed: 1Units: 2Worker 3Speed: 1Units: 1Time: 2×1 = 2Time: 1×1+1×2 = 3Time: 1×1 = 1Workers finish at times: [2, 3, 1]Maximum time (when all work is done): 3
Understanding the Visualization
1
Input
Mountain height=4, workers=[2,1,1] with increasing work per unit
2
Process
Distribute work optimally among workers working simultaneously
3
Output
Minimum time=3 when work is distributed efficiently
Key Takeaway
🎯 Key Insight: Binary search on the total time needed, then verify if workers can collectively finish within that time using optimal work distribution
Asked in
Google 15 Microsoft 12 Amazon 10 Meta 8
28.0K Views
Medium Frequency
~35 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen