2335. Minimum Amount of Time to Fill Cups LeetCode Solution
In this guide, you will get 2335. Minimum Amount of Time to Fill Cups LeetCode Solution with the best time and space complexity. The solution to Minimum Amount of Time to Fill Cups problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Amount of Time to Fill Cups solution in C++
- Minimum Amount of Time to Fill Cups solution in Java
- Minimum Amount of Time to Fill Cups solution in Python
- Additional Resources
Problem Statement of 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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2335. Minimum Amount of Time to Fill Cups LeetCode Solution in C++
class Solution {
public:
int fillCups(vector<int>& amount) {
const int mx = ranges::max(amount);
const int sum = accumulate(amount.begin(), amount.end(), 0);
return max(mx, (sum + 1) / 2);
}
};
/* code provided by PROGIEZ */
2335. Minimum Amount of Time to Fill Cups LeetCode Solution in Java
class Solution {
public int fillCups(int[] amount) {
final int mx = Arrays.stream(amount).max().getAsInt();
final int sum = Arrays.stream(amount).sum();
return Math.max(mx, (sum + 1) / 2);
}
}
// code provided by PROGIEZ
2335. Minimum Amount of Time to Fill Cups LeetCode Solution in Python
class Solution:
def fillCups(self, amount: list[int]) -> int:
return max(max(amount), (sum(amount) + 1) // 2)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.