Leetcode 2335. Minimum Amount of Time to Fill Cups
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 water, warm water, and hot water cups you need to fill respectively. You can fill exactly two different types of cups every second.
Return the minimum number of seconds needed to fill all the cups.
The main idea is to minimize the number of seconds required based on the maximum number of cups for each type:
Here’s the Java implementation of the above strategy:
import java.util.Arrays;
public class MinimumAmountOfTimeToFillCups {
public static int fillCups(int[] amount) {
Arrays.sort(amount);
if (amount[2] >= amount[0] + amount[1]) {
return amount[2];
} else {
return (amount[0] + amount[1] + amount[2] + 1) / 2;
}
}
public static void main(String[] args) {
int[] amount1 = {1, 4, 2};
System.out.println(fillCups(amount1)); // Output: 4
int[] amount2 = {5, 4, 4};
System.out.println(fillCups(amount2)); // Output: 7
}
}
The time complexity of this solution is O(1) since:
Thus, the overall complexity is constant time O(1).
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?