Minimum Number of Seconds to Make Mountain Height Zero Problem
Description
LeetCode Problem 3296.
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] * 2seconds, - ...
- reducing the
x-th unit takesworkerTimes[i] * xseconds.
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:
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] = 2 seconds.
- Worker 1 reduces the height by 2, taking workerTimes[1] + workerTimes[1] * 2 = 3 seconds.
- Worker 2 reduces the height by 1, taking 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:
- Worker 0 reduces the height by 2, taking workerTimes[0] + workerTimes[0] * 2 = 9 seconds.
- Worker 1 reduces the height by 3, taking workerTimes[1] + workerTimes[1] * 2 + workerTimes[1] * 3 = 12 seconds.
- Worker 2 reduces the height by 3, taking workerTimes[2] + workerTimes[2] * 2 + workerTimes[2] * 3 = 12 seconds.
- Worker 3 reduces the height by 2, taking 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 <= 10^51 <= workerTimes.length <= 10^41 <= workerTimes[i] <= 10^6
Solution
Approach: Binary Search on the Answer
If the workers can finish in t seconds, they can finish in any larger time, so binary search the smallest feasible t.
To check a time t, look at each worker separately. A worker with time w who reduces the height by k needs w * (1 + 2 + ... + k) = w * k * (k + 1) / 2 seconds. So the most they can do within t is the largest k with w * k * (k + 1) / 2 <= t. Take k from the square-root formula and then fix it by one or two steps to avoid floating point mistakes. The time t is enough when the sum of all k reaches mountainHeight.
The search range is from 1 up to what the fastest worker needs alone: min(workerTimes) * mountainHeight * (mountainHeight + 1) / 2.
Complexity:
- Time complexity:
O(n log M), whereMis the upper bound of the search - Space complexity:
O(1)
from math import sqrt
class Solution:
def minNumberOfSeconds(self, mountainHeight: int, workerTimes: List[int]) -> int:
def check(t: int) -> bool:
h = 0
for wt in workerTimes:
# largest k with wt * k * (k + 1) / 2 <= t
k = int(sqrt(2 * t / wt + 0.25) - 0.5)
while wt * ((k + 1) * (k + 2) // 2) <= t:
k += 1
while k > 0 and wt * (k * (k + 1) // 2) > t:
k -= 1
h += k
if h >= mountainHeight:
return True
return False
l, r = 1, min(workerTimes) * (mountainHeight * (mountainHeight + 1) // 2)
while l < r:
mid = (l + r) >> 1
if check(mid):
r = mid
else:
l = mid + 1
return l