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

Minimum Amount of Time to Fill Cups

Number: 2412

Difficulty: Easy

Paid? No

Companies: N/A


Problem Description

Given an array of three integers representing the number of cups that need to be filled with cold, warm, and hot water respectively, determine the minimum number of seconds required to fill all cups. Every second, you can either fill two cups with different types of water or one cup of any type.


Key Insights

  • The key observation is that the answer is determined by the maximum between the largest single cup requirement and half of the total cups (rounded up).
  • Pairing cups of different types allows filling two cups in one second, effectively reducing time when possible.
  • When one type has significantly more cups than the sum of the other two, the extra cups have to be filled individually.
  • The final formula becomes: answer = max(max_cups, ceil(total_cups/2)).

Space and Time Complexity

Time Complexity: O(1)
Space Complexity: O(1)


Solution

We begin by calculating the total number of cups to fill and the maximum cups required for any water type. The minimum seconds required is the greater of these two values: the largest count (which dictates the fill rate if one type is dominant) and the ceiling of half the total cups (which is the optimal scenario when cups can be paired). This greedy approach ensures that we are always utilizing the option to fill two cups (from different types) whenever possible, and is effective due to the constant size of the input.


Code Solutions

def fillCups(amount):
    # Calculate the total number of cups to fill.
    total = sum(amount)
    # Determine the maximum cups required for one water type.
    max_cups = max(amount)
    # The minimum seconds is the maximum of the maximum count and half the total (rounded up).
    return max(max_cups, (total + 1) // 2)

# Example usage:
print(fillCups([1,4,2]))  # Expected output: 4
← Back to All Questions