We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Minimum Number of Seconds to Make Mountain Height Zero

Number: 3496

Difficulty: Medium

Paid? No

Companies: N/A


Problem Description

Given a mountain of certain height and an array of worker times, each worker can reduce the mountain’s height sequentially. To reduce the mountain's height by x units, worker i takes workerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * x seconds, and all workers work simultaneously. Find the minimum number of seconds required so that the combined work of all workers reduces the mountain’s height to 0.


Key Insights

  • Each worker i takes workerTimes[i] * (1 + 2 + … + x) = workerTimes[i] * (x*(x+1)/2) seconds to reduce x units.
  • For a fixed time T, we can compute the maximum height each worker can reduce by solving workerTimes[i] * (x*(x+1)/2) <= T.
  • Use binary search on time T. For each candidate T, sum the maximum height each worker can reduce. If the total is at least the mountainHeight, T is sufficient.
  • Efficiently deriving each worker's contribution for a given T is key for the binary search to handle large input limits.

Space and Time Complexity

Time Complexity: O(N * log(T_max)), where N is the number of workers and T_max is the search space for time (derived from worst-case work scenario). Space Complexity: O(1) additional space (ignoring input storage).


Solution

To solve the problem, we use the following strategy:

  1. Use binary search on the time T, where T is the candidate answer.
  2. For a given T, for each worker i, compute the maximum height they can reduce:
    • This involves finding the largest integer x such that workerTimes[i] * (x*(x+1)/2) <= T.
    • We can find x by solving the quadratic inequality x^2 + x - (2*T)/workerTimes[i] <= 0.
    • The positive root of the quadratic equation is given by x = floor((sqrt(1 + 8*T/workerTimes[i]) - 1) / 2).
  3. Sum up these x values over all workers.
  4. If the total is at least mountainHeight, then T seconds are sufficient; otherwise, increase T.
  5. The binary search converges to the minimum T that satisfies the condition.

Key data structures include a simple loop over the workers for each binary search iteration. The critical algorithmic approach is binary search over time combined with solving a quadratic equation to determine each worker's contribution.


Code Solutions

Below are code solutions in multiple languages.

import math

def minimum_seconds(mountainHeight, workerTimes):
    # Helper function to check if mountain can be reduced within T seconds.
    def can_reduce(T):
        total_reduction = 0
        for time in workerTimes:
            # Solve time * (x*(x+1) / 2) <= T
            # => x^2 + x - (2*T)/time <= 0 
            # Compute discriminant
            disc = 1 + 8 * T / time
            x = int((math.sqrt(disc) - 1) // 2)
            total_reduction += x
            if total_reduction >= mountainHeight:
                return True
        return total_reduction >= mountainHeight

    # Binary search boundaries
    low, high = 0, max(workerTimes) * mountainHeight * (mountainHeight + 1) // 2
    while low < high:
        mid = (low + high) // 2
        if can_reduce(mid):
            high = mid
        else:
            low = mid + 1
    return low

# Example usage
print(minimum_seconds(4, [2,1,1]))  # Output: 3
← Back to All Questions