Minimum Number of Seconds to Make Mountain Height Zero โ€” Algorithm Visualization & Coding Challenge

Choose Your Learning Path

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.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

๐Ÿ“– Passive Learning

Read the full explanation, examples, and starter code at your own pace.

๐ŸŽฏ Challenge Mode

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

๐Ÿง  Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

๐Ÿ“– Select Passive to activate

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

โ€” Written by Saurabh Patil โ€ข B.Tech CSE โ€ข Software Developer

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/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now ๐ŸŽฎ

๐ŸŽฏ Select Challenge to activate

๐Ÿง  Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

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.

DrawToCode โ€” Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

Visualization Boardย |ย Problemsย |ย Aboutย |ย Privacy Policy
EmailLinkedInTwitterInstagramGitHub
ยฉ 2026 DrawToCode. All rights reserved.