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:
x, it takes workerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * x seconds. For example:
workerTimes[i] seconds.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.
Example 1:
Input: mountainHeight = 4, workerTimes = [2,1,1]
Output: 3
Explanation:
One way the height of the mountain can be reduced to 0 is:
workerTimes[0] = 2 seconds.workerTimes[1] + workerTimes[1] * 2 = 3 seconds.workerTimes[2] = 1 second.Since they work simultaneously, the minimum time needed is max(2, 3, 1) = 3 seconds.
Example 2:
Input: mountainHeight = 10, workerTimes = [3,2,2,4]
Output: 12
Explanation:
workerTimes[0] + workerTimes[0] * 2 = 9 seconds.workerTimes[1] + workerTimes[1] * 2 + workerTimes[1] * 3 = 12 seconds.workerTimes[2] + workerTimes[2] * 2 + workerTimes[2] * 3 = 12 seconds.workerTimes[3] + workerTimes[3] * 2 = 12 seconds.The number of seconds needed is max(9, 12, 12, 12) = 12 seconds.
Example 3:
Input: mountainHeight = 5, workerTimes = [1]
Output: 15
Explanation:
There is only one worker in this example, so the answer is workerTimes[0] + workerTimes[0] * 2 + workerTimes[0] * 3 + workerTimes[0] * 4 + workerTimes[0] * 5 = 15.
Constraints:
1 <= mountainHeight <= 1051 <= workerTimes.length <= 1041 <= workerTimes[i] <= 106Problem Overview: You are given a mountain of height H and multiple workers. The j-th unit removed by worker i takes time[i] * j seconds, so the total time for k units becomes time[i] * (k * (k + 1) / 2). The goal is to determine the minimum number of seconds required for all workers combined to reduce the mountain height to zero.
Approach 1: Greedy Simulation with Heap (Priority Queue) (Time: O(H log n), Space: O(n))
Simulate the process of assigning work units to workers in chronological order. Each worker’s next completion time follows an arithmetic pattern: t, 3t, 6t, ... because every additional unit costs j * t seconds. Use a min-heap to track the next available completion event across all workers. Pop the earliest event, assign one unit of work, then push the worker back with the updated completion time. Repeat until H units are removed. This greedy strategy always processes the earliest finishing task first, which mirrors how the real timeline progresses. Useful for understanding the process but becomes slow when H is large.
Approach 2: Binary Search on Time (Optimal) (Time: O(n log T), Space: O(1))
Instead of simulating every unit removal, search for the smallest time T such that workers can remove at least H units in that time. For each worker with base time t, solve the inequality t * (k * (k + 1) / 2) ≤ T to determine the maximum units k they can remove. This comes from the triangular sum of increasing work costs. Rearranging gives a quadratic equation that can be solved using math or binary search per worker. Sum all workers' contributions and check if the total reaches H. Because feasibility is monotonic with respect to time, binary search efficiently finds the minimum valid T.
This approach combines mathematical reasoning with monotonic search. Each feasibility check iterates through workers and computes their maximum removable units in constant time.
Approach 3: Dynamic Programming (Time: O(n * H), Space: O(H))
Dynamic programming can model the problem as distributing H units among workers while minimizing total time. Let dp[h] represent the minimum time needed to remove h units. For each worker, iterate possible units they could remove and update the DP state using the triangular time cost t * (k * (k + 1) / 2). While this approach clearly models the incremental cost structure, its complexity grows quickly with large heights. It is mainly useful for understanding the cost relationship rather than as a production solution.
Recommended for interviews: The binary search approach is what interviewers typically expect. A brute-force or heap simulation shows you understand the timeline of work distribution, but recognizing the monotonic property and converting the worker cost into a mathematical constraint demonstrates stronger algorithmic insight. Mentioning the greedy simulation or heap idea before optimizing to binary search often earns extra credit in interviews.
This approach utilizes binary search to find the minimum possible time required by any worker to reduce the mountain's height to zero. We assume an upper bound on the time as the sum of individual times required if one worker does all the work. We binary search this time and check if it's feasible for all workers to collectively bring down the mountain within this time frame.
The solution defines a helper function, canReduce(), that checks if the mountain can be reduced within a given maximum time per worker using a greedy approach. The primary function minSecondsToReduce() performs binary search on the time to find this minimum feasible time.
Python
JavaScript
Time Complexity: O(n * log(m * h)) where n is the number of workers, m is the maximum worker time, and h is the mountain height.
Space Complexity: O(1), as it uses a constant amount of space.
In this approach, a dynamic programming table is used to compute the minimum time required to reduce a mountain of a certain height using previous calculations. We iteratively build up the solution by considering each worker up to a given height.
This C++ implementation involves initializing a DP table of size mountainHeight + 1 with infinity, except for zero height, where the time is zero. We iterate for each worker's time, updating the table based on the possible reductions using cumulative times.
Time Complexity: O(n * h^2), where n is the number of workers, and h is the mountain height. This can be high for large heights.
Space Complexity: O(h), where h is the mountain height.
| Approach | Complexity |
|---|---|
| Binary Search on Max Time | Time Complexity: O(n * log(m * h)) where n is the number of workers, m is the maximum worker time, and h is the mountain height. Space Complexity: O(1), as it uses a constant amount of space. |
| Dynamic Programming | Time Complexity: O(n * h^2), where n is the number of workers, and h is the mountain height. This can be high for large heights. Space Complexity: O(h), where h is the mountain height. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Simulation with Heap | O(H log n) | O(n) | When height is small and you want a direct timeline simulation |
| Binary Search on Time | O(n log T) | O(1) | Optimal solution for large constraints with monotonic feasibility |
| Dynamic Programming | O(n * H) | O(H) | Useful for conceptual understanding of cost distribution |
Minimum Number of Seconds to Make Mountain Height Zero | Understand WHY | Leetcode 3296 | MIK • codestorywithMIK • 11,207 views views
Watch 9 more video solutions →Practice Minimum Number of Seconds to Make Mountain Height Zero with our built-in code editor and test cases.
Practice on FleetCode