[LeetCode] Minimum Amount of Time to Fill Cups

2335. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int fillCups(vector<int>& A) {
int res = 0;
while(!A.empty()) {
sort(rbegin(A), rend(A));
while(!A.empty() and A.back() == 0) A.pop_back();
if(A.size() == 1) {
res += A[0];
A.pop_back();
} else if(A.size() == 2) {
res += A[1];
A[0] -= A[1];
A.pop_back();
} else if(A.size() == 3) {
res += 1;
A[0] -=1;
A[1] -= 1;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/10/PS/LeetCode/minimum-amount-of-time-to-fill-cups/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.