3296. Minimum Number of Seconds to Make Mountain Height Zero
Description
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] = 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.
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 = 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) = 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] <= 106
Solutions
Solution 1: Binary Search
We notice that if all workers can reduce the mountain height to \(0\) in \(t\) seconds, then for any \(t' > t\), the workers can also reduce the mountain height to \(0\) in \(t'\) seconds. Therefore, we can use binary search to find the minimum \(t\) such that the workers can reduce the mountain height to \(0\) in \(t\) seconds.
We define a function \(\textit{check}(t)\), which indicates whether the workers can reduce the mountain height to \(0\) in \(t\) seconds. Specifically, we iterate through each worker. For the current worker \(\textit{workerTimes}[i]\), assuming they reduce the height by \(h'\) in \(t\) seconds, we can derive the inequality:
Solving the inequality, we get:
We can sum up all the \(h'\) values for the workers to get the total reduced height \(h\). If \(h \geq \textit{mountainHeight}\), it means the workers can reduce the mountain height to \(0\) in \(t\) seconds.
Next, we determine the left boundary of the binary search \(l = 1\). Since there is at least one worker, and each worker's working time does not exceed \(10^6\), to reduce the mountain height to \(0\), it takes at least \((1 + \textit{mountainHeight}) \cdot \textit{mountainHeight} / 2 \cdot \textit{workerTimes}[i] \leq 10^{16}\) seconds. Therefore, we can set the right boundary of the binary search to \(r = 10^{16}\). Then, we continuously halve the interval \([l, r]\) until \(l = r\). At this point, \(l\) is the answer.
The time complexity is \(O(n \times \log M)\), where \(n\) is the number of workers, and \(M\) is the right boundary of the binary search, which is \(10^{16}\) in this problem. The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |