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, or1
cup of any type of water.You are given a 0-indexed integer array
amount
of length3
whereamount[0]
,amount[1]
, andamount[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.
什么时候天晴呀(又在草稿里没发出来)
思路:如果存在两个杯子,那么每次装两杯;如果只存在一个杯子,那么每次装一杯。因此每次选择最大值和次大值进行装杯,尽可能每次装满两个杯子,减小最短时长。那么总时长取决于杯子的最大值maxmaxmax与剩下两个值的和sum−maxsum-maxsum−max的大小关系:
实现
class Solution {public int fillCups(int[] amount) {int max = 0;int sum = 0;for (int num : amount){sum += num;max = Math.max(num, max);}if (max > sum - max) {return max;}else{return sum / 2 + sum % 2;}}
}