|
1 | 1 | // https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups
|
2 |
| -// |
| 2 | +// |
3 | 3 | // 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.
|
4 |
| -// |
| 4 | +// |
5 | 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 |
| -// |
| 6 | +// |
7 | 7 | // **Example 1:**
|
8 |
| -// |
| 8 | +// |
9 | 9 | // ```
|
10 | 10 | // **Input:** amount = [1,4,2]
|
11 | 11 | // **Output:** 4
|
|
16 | 16 | // Second 4: Fill up a warm cup.
|
17 | 17 | // It can be proven that 4 is the minimum number of seconds needed.
|
18 | 18 | // ```
|
19 |
| -// |
| 19 | +// |
20 | 20 | // **Example 2:**
|
21 |
| -// |
| 21 | +// |
22 | 22 | // ```
|
23 | 23 | // **Input:** amount = [5,4,4]
|
24 | 24 | // **Output:** 7
|
|
31 | 31 | // Second 6: Fill up a cold cup, and a warm cup.
|
32 | 32 | // Second 7: Fill up a hot cup.
|
33 | 33 | // ```
|
34 |
| -// |
| 34 | +// |
35 | 35 | // **Example 3:**
|
36 |
| -// |
| 36 | +// |
37 | 37 | // ```
|
38 | 38 | // **Input:** amount = [5,0,0]
|
39 | 39 | // **Output:** 5
|
40 | 40 | // **Explanation:** Every second, we fill up a cold cup.
|
41 | 41 | // ```
|
42 |
| -// |
| 42 | +// |
43 | 43 | // **Constraints:**
|
44 |
| -// |
| 44 | +// |
45 | 45 | // * `amount.length == 3`
|
46 | 46 | // * `0 <= amount[i] <= 100`
|
47 | 47 |
|
48 | 48 | pub fn fill_cups(amount: Vec<i32>) -> i32 {
|
49 |
| - |
| 49 | + let mut res = 0; |
| 50 | + let mut amount_clone = amount.clone(); |
| 51 | + loop { |
| 52 | + if amount_clone.iter().max().unwrap() < &1 { |
| 53 | + return res; |
| 54 | + } |
| 55 | + amount_clone.sort_unstable(); |
| 56 | + amount_clone[2] -= 1; |
| 57 | + amount_clone[1] -= 1; |
| 58 | + res += 1; |
50 | 59 | }
|
| 60 | +} |
51 | 61 |
|
52 | 62 | #[test]
|
53 | 63 | pub fn t1() {
|
| 64 | + assert_eq!(fill_cups(vec![1, 4, 2]), 4); |
| 65 | + assert_eq!(fill_cups(vec![5, 4, 4]), 7); |
54 | 66 | }
|
0 commit comments