aboutsummaryrefslogtreecommitdiff
path: root/min_rounds_complete_all_tasks.rs
blob: 1c9b7dd4449a93f936796a92cc0ac6f0cb0fe363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::collections::HashMap;
struct Solution;

impl Solution {
    pub fn minimum_rounds(tasks: Vec<i32>) -> i32 {
        let (mut map, mut ans) = (HashMap::new(), 0);
        for t in tasks {
            let val = map.get_mut(&t);
            if let Some(v) = val {
                *v += 1;
            } else {
                map.insert(t, 1);
            }
        }
        for c in map.into_values() {
            if c == 1 {
                return -1;
            }
            ans += (c + 2) / 3;
        }
        ans
    }
}

fn main() {
    let tasks1 = vec![2,2,3,3,2,4,4,4,4,4];
    let tasks2 = vec![2,3,3];
    println!("{}", Solution::minimum_rounds(tasks1));
    println!("{}", Solution::minimum_rounds(tasks2));
}