Track & Visualize

Minimum Number of Seconds to Make Mountain Height Zero

Choose Your Learning Path

How would you like to learn today? Visualize algorithms in real time, explore them step by step through reading, or challenge yourself with a test.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations. See exactly how the code executes in real time.

๐Ÿ“– Passive Learning

Read comprehensive problem explanation, reference links, and explore the code at your own pace.

๐ŸŽฏ Mandatory Challenge

Drag and arrange the algorithm steps in the correct execution order.

Understanding Minimum Number of Seconds to Make Mountain Height Zero
Detailed explanation and reference materials
Problem Overview

๐Ÿ”๏ธ 3296. Minimum Number of Seconds to Make Mountain Height Zero

Difficulty: ๐ŸŸก Medium ย |ย  Topics: Binary Search ยท Math ยท Greedy


๐Ÿ“‹ Problem Statement

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.


๐Ÿ” Examples

Example 1

Input : mountainHeight = 4, workerTimes = [2, 1, 1]
Output: 3
WorkerHeight ReducedTime Taken
012s
121 + 2 = 3s
211s

โœ… max(2, 3, 1) = 3 seconds


Example 2

Input : mountainHeight = 10, workerTimes = [3, 2, 2, 4]
Output: 12
WorkerHeight ReducedTime Taken
023 + 6 = 9s
132 + 4 + 6 = 12s
232 + 4 + 6 = 12s
324 + 8 = 12s

โœ… max(9, 12, 12, 12) = 12 seconds


Example 3

Input : mountainHeight = 5, workerTimes = [1]
Output: 15
WorkerHeight ReducedTime Taken
051+2+3+4+5 = 15s

โœ… 15 seconds


๐Ÿ“ Key Formula

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 )

๐Ÿ’ก Approach โ€” Binary Search on Answer

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

โœ… Solution (Java)

java
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

Complexity
TimeO(n ร— log(w ร— Hยฒ))
SpaceO(1)

Where maxTime โ‰ˆ w ร— Hยฒ โ‰ˆ 10^16 โ†’ log factor โ‰ˆ 53 iterations.

  • n = number of workers
  • w = 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


Categories
Binary Search
leetcode-problem-of-the-day
arrays
greedy
heaps-hashing
java
Reference Link
https://leetcode.com/problems/minimum-number-of-seconds-to-make-mountain-height-zero/description/
Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.

Loading component...

Java
Output:
Understood Algorithm , Test Me now ๐ŸŽฎ
JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time, so you understand not just the result, but the journey.

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time, so you understand not just the result, but the journey.

๐Ÿง  Logic Puzzle
Arrange the Algorithm Correctly ๐Ÿงฉ

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 Algorithm

Don't Know Current Algorithm ? ย 

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

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.

ยฉ 2026 | Privacy Policy | About