https://leetcode.cn/problems/minimum-amount-of-time-to-fill-cups/
如果需要時(shí)長最短,當(dāng)然要盡可能多的滿足在同一時(shí)間段內(nèi),可以裝兩杯水搀暑。同時(shí)忙芒,有一個(gè)需要注意的問題舱呻,假設(shè)溫水有100杯届惋,熱水加冷水一共20杯瞬哼,那么這時(shí)候還需要計(jì)算么份名?不需要碟联,因?yàn)闆]有可優(yōu)化的空間了妓美,所以直接返回100就好±鸱酰可以大大降低程序耗時(shí)壶栋。所以就保留三個(gè)數(shù)字的最大值,然后消耗其他兩個(gè)數(shù)字普监,直到其他兩個(gè)數(shù)字之和小于最大值贵试,直接返回消耗的次數(shù)+最大值即可。
上代碼
class Solution {
public int fillCups(int[] amount) {
int max=amount[0];
if(amount[1]>max){
max=amount[1];
}
if(amount[2]>max){
max=amount[2];
}
int sum=0;
for (int i : amount) {
sum=sum+i;
}
sum=sum-max;
if(max>sum){
return max;
}
int count=0;
while(max<sum){
sum=sum-2;
count++;
}
return count+max;
}
}