作者:小迅
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目
![](https://i-blog.csdnimg.cn/blog_migrate/475fde027eeb5780165a9174f005bda3.png)
示例
![](https://i-blog.csdnimg.cn/blog_migrate/bfe9f0df1cc4168306cb1b6507ef5dfb.png)
思路
每次充水肯定一次充2是更优的,我们尽可能多的充2,这样只需要总和一半的次数就能完成。
但是什么情况下不能呢,就是在一个比另外两者加起来还要多的情况下。
此时整体来看,当这个多的充满,另外两个肯定也完事了,因此次数等于这个最大需求
代码注释超级详细
代码
static int cmp(const void *pa, const void *pb) {//排序
return *(int *)pa - *(int *)pb;
}
int fillCups(int* amount, int amountSize) {
qsort(amount, amountSize, sizeof(int), cmp);//数组升序
if (amount[2] > amount[1] + amount[0]) {//特殊情况
return amount[2];
}
return (amount[0] + amount[1] + amount[2] + 1) / 2;//一般情况
}
作者:小迅
链接:https://leetcode.cn/problems/minimum-amount-of-time-to-fill-cups/solutions/2105151/tan-xin-zhu-shi-chao-ji-xiang-xi-by-xun-vi8o9/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。