|
| 1 | +# [Problem 3296: Minimum Number of Seconds to Make Mountain Height Zero](https://leetcode.com/problems/minimum-number-of-seconds-to-make-mountain-height-zero/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +We need to distribute mountainHeight units of "work" among workers. If worker i does x units, their time is workerTimes[i] * (1 + 2 + ... + x) = workerTimes[i] * x*(x+1)/2. Workers operate simultaneously, so the total time is the maximum time any worker spends; we want to minimize that maximum subject to sum x_i = mountainHeight. This looks like a feasibility check: for a given time T, each worker can do at most k_i units where workerTimes[i] * k_i*(k_i+1)/2 <= T. If sum k_i >= mountainHeight, T is feasible. So binary search on T is natural. Need to compute k_i efficiently (solve quadratic or use integer math). Watch out for large numbers (T up to ~5e15), use integer sqrt (math.isqrt) to avoid floating point error. |
| 5 | + |
| 6 | +## Refining the problem, round 2 thoughts |
| 7 | +- For a candidate T, compute M = 2*T // workerTimes[i], solve k^2 + k <= M => k = floor((-1 + sqrt(1 + 4*M))/2). Using integer math (math.isqrt) is safe and exact. |
| 8 | +- Early exit the capacity sum when it reaches mountainHeight. |
| 9 | +- Binary search bounds: low = 0, high = max(workerTime) * mountainHeight*(mountainHeight+1)//2 (one worker does everything) — fits in 64-bit integer for given constraints. |
| 10 | +- Complexity: O(n * log(maxT)). With n <= 1e4 and log(maxT) ~ ~52, this is fine. |
| 11 | +- Edge cases: single worker, many workers faster than needed (sum capacities exceed height quickly), and very large workerTimes produce small capacities for given T. |
| 12 | + |
| 13 | +## Attempted solution(s) |
| 14 | +```python |
| 15 | +import math |
| 16 | +from typing import List |
| 17 | + |
| 18 | +class Solution: |
| 19 | + def minimumTime(self, mountainHeight: int, workerTimes: List[int]) -> int: |
| 20 | + # Helper: given time T, compute total units of height workers can reduce |
| 21 | + def capacity(T: int) -> int: |
| 22 | + total = 0 |
| 23 | + for w in workerTimes: |
| 24 | + if T < w: # can't even do 1 unit |
| 25 | + continue |
| 26 | + # Solve k*(k+1)/2 * w <= T => k^2 + k <= 2*T//w (use integer division) |
| 27 | + M = (2 * T) // w |
| 28 | + # discriminant for k^2 + k - M <= 0 is 1 + 4*M |
| 29 | + d = 1 + 4 * M |
| 30 | + sqrt_d = math.isqrt(d) |
| 31 | + k = (sqrt_d - 1) // 2 |
| 32 | + total += k |
| 33 | + if total >= mountainHeight: |
| 34 | + return total |
| 35 | + return total |
| 36 | + |
| 37 | + # Binary search on time T |
| 38 | + lo = 0 |
| 39 | + # Upper bound: slowest single worker doing all mountainHeight |
| 40 | + max_w = max(workerTimes) |
| 41 | + hi = max_w * mountainHeight * (mountainHeight + 1) // 2 |
| 42 | + |
| 43 | + while lo < hi: |
| 44 | + mid = (lo + hi) // 2 |
| 45 | + if capacity(mid) >= mountainHeight: |
| 46 | + hi = mid |
| 47 | + else: |
| 48 | + lo = mid + 1 |
| 49 | + return lo |
| 50 | +``` |
| 51 | +- Notes about the solution: |
| 52 | + - Approach: binary search on time T; for each T compute how many units each worker can remove using integer solution of quadratic inequality. |
| 53 | + - Time complexity: O(n * log U) where U is the search space upper bound (roughly max worker time * H*(H+1)/2). With constraints this is efficient (n ≤ 1e4, log U ≲ 60). |
| 54 | + - Space complexity: O(1) extra space (besides input). |
| 55 | + - Implementation detail: use math.isqrt on integer discriminant to avoid floating-point inaccuracies and ensure correctness for large values. Early exit when accumulated capacity reaches mountainHeight to save work. |
0 commit comments