LeetCode 2335: Minimum Amount of Time to Fill Cups Solution

Master LeetCode problem 2335 (Minimum Amount of Time to Fill Cups), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2335. Minimum Amount of Time to Fill Cups

Problem Explanation

Explanation:

To solve this problem, we need to minimize the time it takes to fill all the cups. We can observe that filling up 2 cups with different types of water (2 seconds) is more efficient than filling up 1 cup of any type of water (1 second). Therefore, we should prioritize filling up 2 cups with different types of water whenever possible.

The algorithmic idea is to keep filling up 2 cups with different types of water until one type of cup runs out, then switch to filling up 1 cup at a time until all cups are filled. We will keep track of the remaining cups and the current time taken.

At each step, we will check which type of cup has the most remaining cups, and fill up 2 cups with different types of water if possible. If not, we fill up 1 cup of any type. We continue this process until all cups are filled.

:

Solution Code

class Solution {
    public int minSecondsToFillCups(int[] amount) {
        int totalSeconds = 0;
        while (amount[0] > 0 && amount[1] > 0 && amount[2] > 0) {
            amount[0]--;
            amount[1]--;
            amount[2]--;
            totalSeconds += 2;
        }
        while (amount[0] > 0 || amount[1] > 0 || amount[2] > 0) {
            for (int i = 0; i < 3; i++) {
                if (amount[i] > 0) {
                    amount[i]--;
                    totalSeconds++;
                }
            }
        }
        return totalSeconds;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2335 (Minimum Amount of Time to Fill Cups)?

This page provides optimized solutions for LeetCode problem 2335 (Minimum Amount of Time to Fill Cups) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2335 (Minimum Amount of Time to Fill Cups)?

The time complexity for LeetCode 2335 (Minimum Amount of Time to Fill Cups) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2335 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2335 in Java, C++, or Python.

Back to LeetCode Solutions