Minimum Number of Seconds to Make Mountain Height Zero - Problem

Imagine you're the manager of a mountain demolition project where multiple workers need to completely level a mountain to height zero. Each worker has different efficiency levels and works simultaneously with others.

You are given:

  • mountainHeight - an integer representing the initial height of the mountain
  • workerTimes - an array where workerTimes[i] represents the base time (in seconds) for worker i

The Challenge: Each worker becomes progressively slower with each unit of height they remove. Specifically, for worker i to reduce the mountain height by x units total, they need:

workerTimes[i] × 1 + workerTimes[i] × 2 + ... + workerTimes[i] × x seconds

This equals workerTimes[i] × (1 + 2 + ... + x) = workerTimes[i] × x × (x + 1) / 2 seconds

Goal: Find the minimum number of seconds needed for all workers combined to reduce the mountain height to exactly zero.

Input & Output

example_1.py — Basic Example
$ Input: mountainHeight = 4, workerTimes = [2, 1, 1]
Output: 3
💡 Note: Worker assignments: Worker 1 does 1 unit (2×1×2/2 = 2 sec), Worker 2 does 2 units (1×2×3/2 = 3 sec), Worker 3 does 1 unit (1×1×2/2 = 1 sec). Maximum time is 3 seconds.
example_2.py — Single Worker
$ Input: mountainHeight = 3, workerTimes = [5]
Output: 30
💡 Note: Only one worker available. Time needed = 5×3×4/2 = 30 seconds to remove all 3 units of height.
example_3.py — Equal Workers
$ Input: mountainHeight = 6, workerTimes = [3, 3, 3]
Output: 18
💡 Note: Each worker does 2 units. Time per worker = 3×2×3/2 = 9 seconds. Since they work simultaneously, total time is 9 seconds. Wait, let me recalculate: each worker can do 2 units in 3×(1+2) = 9 seconds, but we need to verify this is optimal.

Constraints

  • 1 ≤ mountainHeight ≤ 105
  • 1 ≤ workerTimes.length ≤ 104
  • 1 ≤ workerTimes[i] ≤ 106
  • All workers work simultaneously

Visualization

Tap to expand
Mountain Demolition ProblemMountain Height: 4Worker 1Time: 2Worker 2Time: 1Worker 3Time: 1Work Distribution AnalysisWorker 1: 1 unit → 2×1×2/2 = 2 secWorker 2: 2 units → 1×2×3/2 = 3 secWorker 3: 1 unit → 1×1×2/2 = 1 secTotal work: 1+2+1 = 4 ✓Max time: max(2,3,1) = 3 secOptimal Solution: 3 seconds💡 Key: Binary search finds minimum time where sum of worker capacities ≥ mountain height
Understanding the Visualization
1
Setup
Multiple workers positioned at different parts of the mountain
2
Progressive Fatigue
Each worker's nth unit takes n times their base time
3
Binary Search
Find minimum time by testing if workers can finish within given time limits
4
Optimal Assignment
Greedily assign maximum possible work to each worker within time constraint
Key Takeaway
🎯 Key Insight: Binary search on the answer combined with greedy capacity calculation gives us the optimal O(n log T) solution, where T is the maximum possible time.
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
34.2K Views
Medium Frequency
~25 min Avg. Time
1.5K 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