2335. Minimum Amount of Time to Fill Cups

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int fillCups(int[] amount) {
// 目标:尽量匹配不同的饮料
Arrays.sort(amount);
if (amount[1] == 0) {
// 只剩热水杯子
return amount[2];
}
// 剩温水杯子和热水杯子,可能剩冷水杯子,此时使用贪心策略,优先装最需要装满的杯子
amount[1]--;
amount[2]--;
return 1 + fillCups(amount);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int fillCups(int[] amount) {
Arrays.sort(amount);

if (amount[0] + amount[1] <= amount[2]) {
return amount[2];
} else {
int t = amount[0] + amount[1] - amount[2];
if ((t & 1) == 0) {
// t 为偶数
return t / 2 + amount[2];
} else {
// t 为奇数
return (t - 1) / 2 + amount[2] + 1;
}
}
}
}

References

2335. Minimum Amount of Time to Fill Cups