Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2335.html

2335. Minimum Amount of Time to Fill Cups

  • Difficulty: Easy.
  • Related Topics: Array, Greedy, Heap (Priority Queue).
  • Similar Questions: Construct Target Array With Multiple Sums, Maximum Score From Removing Stones, Maximum Running Time of N Computers.

Problem

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

Solution (Java, C++, Python)

  • class Solution {
        public int fillCups(int[] amount) {
            Arrays.sort(amount);
            int sum = amount[0] + amount[1] + amount[2];
            return (amount[0] + amount[1] < amount[2]) ? amount[2] : (sum + 1) / 2;
        }
    }
    
    ############
    
    class Solution {
        public int fillCups(int[] amount) {
            Arrays.sort(amount);
            if (amount[0] + amount[1] <= amount[2]) {
                return amount[2];
            }
            return (amount[0] + amount[1] + amount[2] + 1) / 2;
        }
    }
    
  • class Solution:
        def fillCups(self, amount: List[int]) -> int:
            amount.sort()
            if amount[0] + amount[1] <= amount[2]:
                return amount[2]
            return (sum(amount) + 1) // 2
    
    ############
    
    # 2335. Minimum Amount of Time to Fill Cups
    # https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/
    
    class Solution:
        def fillCups(self, amount: List[int]) -> int:
            res = 0
            H = [-x for x in amount if x != 0]
            heapify(H)
            
            while H:
                if len(H) >= 2:
                    first, second = heappop(H), heappop(H)
                    first = -first
                    second = -second
                    
                    first -= 1
                    second -= 1
                    if first > 0:
                        heappush(H, -first)
                    if second > 0:
                        heappush(H, -second)
                    res += 1
                else:
                    res += -heappop(H)
            
            return res
    
    
  • class Solution {
    public:
        int fillCups(vector<int>& amount) {
            sort(amount.begin(), amount.end());
            if (amount[0] + amount[1] <= amount[2]) {
                return amount[2];
            }
            return (amount[0] + amount[1] + amount[2] + 1) / 2;
        }
    };
    
  • func fillCups(amount []int) int {
    	sort.Ints(amount)
    	if amount[0]+amount[1] <= amount[2] {
    		return amount[2]
    	}
    	return (amount[0] + amount[1] + amount[2] + 1) / 2
    }
    
  • function fillCups(amount: number[]): number {
        amount.sort((a, b) => a - b);
        let [a, b, c] = amount;
        let diff = a + b - c;
        if (diff <= 0) return c;
        else return Math.floor((diff + 1) / 2) + c;
    }
    
    
  • impl Solution {
        pub fn fill_cups(mut amount: Vec<i32>) -> i32 {
            amount.sort();
            let dif = amount[0] + amount[1] - amount[2];
            if dif <= 0 {
                return amount[2];
            }
            (dif + 1) / 2 + amount[2]
        }
    }
    
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions