Minimum Amount of Time to Fill Cups

🏠 ⬅️ ➡️

You have a water dispenser that can dispense cold, warm, and hot water. Every second, you can either fill up 2 cups with different types of water, or 1 cup of any type of water.

You are given a 0-indexed integer array amount of length 3 where amount[0], amount[1], and amount[2] denote the number of cold, warm, and hot water cups you need to fill respectively. Return the minimum number of seconds needed to fill up all the cups.

Example 1:

Input: amount = [1,4,2] Output: 4 Explanation: One way to fill up the cups is: Second 1: Fill up a cold cup and a warm cup. Second 2: Fill up a warm cup and a hot cup. Second 3: Fill up a warm cup and a hot cup. Second 4: Fill up a warm cup. It can be proven that 4 is the minimum number of seconds needed.

Example 2:

Input: amount = [5,4,4] Output: 7 Explanation: One way to fill up the cups is: Second 1: Fill up a cold cup, and a hot cup. Second 2: Fill up a cold cup, and a warm cup. Second 3: Fill up a cold cup, and a warm cup. Second 4: Fill up a warm cup, and a hot cup. Second 5: Fill up a cold cup, and a hot cup. Second 6: Fill up a cold cup, and a warm cup. Second 7: Fill up a hot cup.

Example 3:

Input: amount = [5,0,0] Output: 5 Explanation: Every second, we fill up a cold cup.

Constraints:

  • amount.length == 3
  • 0 <= amount[i] <= 100

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i, j, k, n, m, l
    integer, parameter :: amount(3) = [1, 4, 2]

    write (*, '(I0)') solve(amount)

contains

    function solve(amount) result(res)
        implicit none
        integer, intent(in) :: amount(:)
        integer :: res

        res = minval([(/ (i + 1) * (j + 1) * (k + 1), &
                         i = 0, amount(1) - 1, 1 ), &
                       (/ (i + 1) * (j + 1), &
                          i = 0, amount(2) - 1, 1 ), &
                       (/ (i + 1), &
                          i = 0, amount(3) - 1, 1 ) /)])
    end function solve

end program main
Compiled
Executed
⚠️ Correct
module water_dispenser
    implicit none
    private
    public :: solve
contains
    function solve(amount) result(min_seconds)
        integer, intent(in) :: amount(3)
        integer :: min_seconds
        integer :: cold, warm, hot
        integer :: seconds

        cold = amount(1)
        warm = amount(2)
        hot = amount(3)

        ! Initialize the minimum number of seconds to fill up all the cups
        min_seconds = 0

        ! Loop until all the cups are filled
        do while (cold > 0 .or. warm > 0 .or. hot > 0)
            ! Fill up 2 cups with different types of water
            if (cold > 0 .and. warm > 0) then
                cold = cold - 2
                warm = warm - 2
                min_seconds = min_seconds + 2
            ! Fill up 1 cup with any type of water
            else if (cold > 0 .or. warm > 0 .or. hot > 0) then
                if (cold > 0) then
                    cold = cold - 1
                    min_seconds = min_seconds + 1
                else if (warm > 0) then
                    warm = warm - 1
                    min_seconds = min_seconds + 1
                else
                    hot = hot - 1
                    min_seconds = min_seconds + 1
                end if
            end if
        end do
    end function solve
end module water_dispenser

! Test the solve function with the given examples
program test_water_dispenser
    use water_dispenser
    implicit none
    integer :: amount(3)
    integer :: min_seconds

    ! Example 1: Fill up 2 cups with different types of water
    amount = [1, 4, 2]
    min_seconds = solve(amount)
    write (*,*) "Example 1: Minimum number of seconds to fill up all the cups = ", min_seconds

    ! Example 2: Fill up 1 cup with any type of water
    amount = [5, 4, 4]
    min_seconds = solve(amount)
    write (*,*) "Example 2: Minimum number of seconds to fill up all the cups = ", min_seconds

    ! Example 3: Fill up all cups with cold water
    amount = [5, 0, 0]
    min_seconds = solve(amount)
    write (*,*) "Example 3: Minimum number of seconds to fill up all the cups = ", min_seconds
end program test_water_dispenser
🌐 Data from online sources
def min_seconds(amount: List[int]) -> int:
    total = sum(amount)
    largest_two = max(amount[0] + amount[1], amount[1] + amount[2], amount[0] + amount[2])
    return (total + 1) // 2 - (largest_two + 1) // 2 + largest_two

First, calculate the total number of cups required (total) and the combined amount of the two largest amounts of cups (largest_two). The minimum number of seconds needed is the sum of half the total (rounded up), minus half the largest_two (rounded up) plus largest_two. This should give us the minimum number of seconds to fill up all the cups.

🌐 Data from online sources
int min_seconds(vector<int>& amount) {
    int total = accumulate(amount.begin(), amount.end(), 0);
    int largest_two = max(amount[0] + amount[1], max(amount[1] + amount[2], amount[0] + amount[2]));
    return (total + 1) / 2 - (largest_two + 1) / 2 + largest_two;
}

First, calculate the total number of cups required (total) and the combined amount of the two largest amounts of cups (largest_two). The minimum number of seconds needed is the sum of half the total (rounded up), minus half the largest_two (rounded up) plus largest_two. This should give us the minimum number of seconds to fill up all the cups.