2335. Minimum Amount of Time to Fill Cups Easy
1/**
2 * [2335] Minimum Amount of Time to Fill Cups
3 *
4 * 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.
5 * 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.
6 *
7 * Example 1:
8 *
9 * Input: amount = [1,4,2]
10 * Output: 4
11 * Explanation: One way to fill up the cups is:
12 * Second 1: Fill up a cold cup and a warm cup.
13 * Second 2: Fill up a warm cup and a hot cup.
14 * Second 3: Fill up a warm cup and a hot cup.
15 * Second 4: Fill up a warm cup.
16 * It can be proven that 4 is the minimum number of seconds needed.
17 *
18 * Example 2:
19 *
20 * Input: amount = [5,4,4]
21 * Output: 7
22 * Explanation: One way to fill up the cups is:
23 * Second 1: Fill up a cold cup, and a hot cup.
24 * Second 2: Fill up a cold cup, and a warm cup.
25 * Second 3: Fill up a cold cup, and a warm cup.
26 * Second 4: Fill up a warm cup, and a hot cup.
27 * Second 5: Fill up a cold cup, and a hot cup.
28 * Second 6: Fill up a cold cup, and a warm cup.
29 * Second 7: Fill up a hot cup.
30 *
31 * Example 3:
32 *
33 * Input: amount = [5,0,0]
34 * Output: 5
35 * Explanation: Every second, we fill up a cold cup.
36 *
37 *
38 * Constraints:
39 *
40 * amount.length == 3
41 * 0 <= amount[i] <= 100
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/
47// discuss: https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50use std::cmp;
51impl Solution {
52 pub fn fill_cups(amount: Vec<i32>) -> i32 {
53 let (mut sum, mut max) = (0, 0);
54 for i in amount {
55 sum += i;
56 max = cmp::max(max, i);
57 }
58 cmp::max(max, (sum + 1) / 2)
59 }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn test_2335() {
70 assert_eq!(Solution::fill_cups(vec![5,4,4]), 7);
71 }
72}
73
Back
© 2025 bowen.ge All Rights Reserved.