3296. Minimum Number of Seconds to Make Mountain Height Zero
Difficulty: Medium
Topics: Staff, Array, Math, Binary Search, Greedy, Heap (Priority Queue), Weekly Contest 416
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 takesworkerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * xseconds. 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] * 2seconds, and so on.
- To reduce the height of the mountain by 1, it takes
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:
- Worker 0 reduces the height by 1, taking
workerTimes[0] = 2seconds. - Worker 1 reduces the height by 2, taking
workerTimes[1] + workerTimes[1] * 2 = 3seconds. - Worker 2 reduces the height by 1, taking
workerTimes[2] = 1second.
- Worker 0 reduces the height by 1, taking
- Since they work simultaneously, the minimum time needed is
max(2, 3, 1) = 3seconds.
- One way the height of the mountain can be reduced to 0 is:
Example 2:
- Input: mountainHeight = 10, workerTimes = [3,2,2,4]
- Output: 12
-
Explanation:
- Worker 0 reduces the height by 2, taking
workerTimes[0] + workerTimes[0] * 2 = 9seconds. - Worker 1 reduces the height by 3, taking
workerTimes[1] + workerTimes[1] * 2 + workerTimes[1] * 3 = 12seconds. - Worker 2 reduces the height by 3, taking
workerTimes[2] + workerTimes[2] * 2 + workerTimes[2] * 3 = 12seconds. - Worker 3 reduces the height by 2, taking
workerTimes[3] + workerTimes[3] * 2 = 12seconds. - The number of seconds needed is
max(9, 12, 12, 12) = 12seconds.
- Worker 0 reduces the height by 2, taking
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 <= 10⁵1 <= workerTimes.length <= 10⁴1 <= workerTimes[i] <= 10⁶
Hint:
- Can we use binary search to solve this problem?
- Do a binary search on the number of seconds to check if it's enough to reduce the mountain height to 0 or less with all workers working simultaneously.
Solution:
The problem asks for the minimum number of seconds required for multiple workers, each with a given base time, to reduce a mountain of a given height to zero. Workers work simultaneously and the time for a worker to reduce the height by x units is the sum of an arithmetic progression: workerTimes[i] * (1 + 2 + ... + x) = workerTimes[i] * x * (x + 1) / 2. The solution uses binary search on the total time and a greedy feasibility check to see if all workers together can reduce the required height within that time.
Approach:
-
Binary search the answer between
1and a safe upper bound (when one worker does all the work:max(workerTimes) * mountainHeight * (mountainHeight + 1) / 2). - For a candidate time
T, determine if it's possible to reduce the mountain to zero:- For each worker, compute the maximum number of units that worker can contribute within
Tseconds. - This is done via another binary search on
x(units) for that worker, solvingworkerTimes[i] * x * (x + 1) / 2 ≤ T. - Sum the units from all workers. If the total is at least
mountainHeight, thenTis feasible.
- For each worker, compute the maximum number of units that worker can contribute within
- Use the feasibility result to adjust the binary search bounds: if feasible, try smaller
T; otherwise, increaseT. - Return the smallest feasible time.
Let's implement this solution in PHP: 3296. Minimum Number of Seconds to Make Mountain Height Zero
<?php
/**
* @param Integer $mountainHeight
* @param Integer[] $workerTimes
* @return Integer
*/
function minNumberOfSeconds(int $mountainHeight, array $workerTimes): int
{
...
...
...
/**
* go to ./solution.php
*/
}
/**
* Check whether it is possible to reduce the mountain height to zero within T seconds.
*
* @param int $T
* @param int $H
* @param array $workerTimes
* @return bool
*/
function canReduce(int $T, int $H, array $workerTimes): bool {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo minNumberOfSeconds(4, [2,1,1]) . "\n"; // Output: 3
echo minNumberOfSeconds(10, [3,2,2,4]) . "\n"; // Output: 12
echo minNumberOfSeconds(5, [1]) . "\n"; // Output: 15
?>
Explanation:
-
Why binary search on time? The required time is monotonic: if we can finish in
Tseconds, we can certainly finish in any larger time. Hence we can binary search for the minimumT. -
Upper bound calculation: A loose but safe upper bound is when the slowest worker does all the work:
max(workerTimes) * (1 + 2 + ... + mountainHeight) = max(workerTimes) * mountainHeight * (mountainHeight + 1) / 2. This ensures the search space is correct. -
Feasibility check (
canReduce):- For a given worker with base time
wand time limitT, we want the largestxsuch thatw * x * (x + 1) / 2 ≤ T. This is a quadratic inequality; we solve it with a binary search onx(0 to mountainHeight) because the sum grows monotonically withx. - The product
w * x * (x + 1)is divided by 2. Sincex * (x + 1)is always even, the division yields an integer, avoiding floating-point issues. - Accumulate the total possible units across all workers. As soon as the total reaches or exceeds
mountainHeight, we can returntrueearly.
- For a given worker with base time
-
Binary search on
xfor each worker: In the worst case we perform aboutlog₂(mountainHeight)steps per worker, which is efficient given the constraints (mountainHeight ≤ 1e5, workers ≤ 1e4).
Complexity
-
Time complexity:
- Outer binary search:
O(log (maxTime))wheremaxTime ≈ max(workerTimes) * H²(aboutlog(1e6 * 1e10) ≈ 55steps). - For each candidate
T, we iterate over allnworkers and perform a binary search onx(range 0..H):O(n * log H). - Overall:
O(log(maxTime) * n * log H). WithH ≤ 1e5,n ≤ 1e4, this is acceptable.
- Outer binary search:
-
Space complexity:
O(1)additional space (excluding input storage).
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)