How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Difficulty: ๐ก Medium ย |ย Topics: Binary Search ยท Math ยท Greedy
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 takes:
workerTimes[i] ร 1 + workerTimes[i] ร 2 + ... + workerTimes[i] ร x
= workerTimes[i] ร (x ร (x + 1) / 2) seconds
๐ฏ Return the minimum number of seconds required for all workers to reduce the height to 0.
Input : mountainHeight = 4, workerTimes = [2, 1, 1]
Output: 3
| Worker | Height Reduced | Time Taken |
|---|---|---|
| 0 | 1 | 2s |
| 1 | 2 | 1 + 2 = 3s |
| 2 | 1 | 1s |
โ
max(2, 3, 1) = 3 seconds
Input : mountainHeight = 10, workerTimes = [3, 2, 2, 4]
Output: 12
| Worker | Height Reduced | Time Taken |
|---|---|---|
| 0 | 2 | 3 + 6 = 9s |
| 1 | 3 | 2 + 4 + 6 = 12s |
| 2 | 3 | 2 + 4 + 6 = 12s |
| 3 | 2 | 4 + 8 = 12s |
โ
max(9, 12, 12, 12) = 12 seconds
Input : mountainHeight = 5, workerTimes = [1]
Output: 15
| Worker | Height Reduced | Time Taken |
|---|---|---|
| 0 | 5 | 1+2+3+4+5 = 15s |
โ
15 seconds
The cost for a worker with time w to reduce height by x:
cost(w, x) = w ร x ร (x + 1) / 2
Given a time budget T, max height a worker can reduce:
solve: w ร x ร (x + 1) / 2 โค T
โ x ร (x + 1) โค 2T / w
โ x โ floor( (-1 + sqrt(1 + 8T/w)) / 2 )
1. Binary search on time T in range [0, max_cost]
2. For each T, greedily compute max height each worker can reduce
3. Check if total reductions โฅ mountainHeight
4. Minimize T
class Solution {
public long minNumberOfSeconds(int mountainHeight, int[] workerTimes) {
// โ
Compute tight upper bound: worst worker does all the work
long maxTime = Long.MIN_VALUE;
for (int w : workerTimes) {
long h = mountainHeight;
long cost = (long) w * h * (h + 1) / 2;
maxTime = Math.max(maxTime, cost);
}
long lo = 0, hi = maxTime;
while (lo < hi) {
long mid = lo + (hi - lo) / 2;
if (canFinish(mid, mountainHeight, workerTimes))
hi = mid;
else
lo = mid + 1;
}
return lo;
}
private boolean canFinish(long time, int height, int[] workerTimes) {
long total = 0;
for (int w : workerTimes) {
long x = (long)((-1 + Math.sqrt(1 + 8.0 * time / w)) / 2);
// โ
Correct floating point rounding errors
while ((long) w * (x + 1) * (x + 2) / 2 <= time) x++;
while (x > 0 && (long) w * x * (x + 1) / 2 > time) x--;
total += x;
if (total >= height) return true;
}
return total >= height;
}
}
| Complexity | |
|---|---|
| Time | O(n ร log(w ร Hยฒ)) |
| Space | O(1) |
Where maxTime โ w ร Hยฒ โ 10^16 โ log factor โ 53 iterations.
n = number of workersw = max value in workerTimes (up to 10โถ)Hยฒ = shorthand for H ร (H+1) / 2 where H = mountainHeight (up to 10โต) โ search range scales quadratically with height๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.