#2244
Medium Algorithms Minimum rounds to complete all tasks
Array Hash Table Greedy Counting
63.2% acceptance
Feb 25, 2026
2849
85
You are given a 0-indexed integer array tasks, where tasks[i] represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the same difficulty level.
Return the minimum rounds required to complete all the tasks, or -1 if it is not possible to complete all the tasks.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn minimum_rounds(tasks: Vec<i32>) -> i32 {
use std::collections::HashMap;
let mut freq: HashMap<i32, i32> = HashMap::new();
for t in tasks {
*freq.entry(t).or_default() += 1;
}
let mut rounds = 0;
for &count in freq.values() {
if count == 1 {
return -1;
}
// Minimum rounds: prefer groups of 3, if count%3==1 use one group of 2
rounds += (count + 2) / 3;
}
rounds
}
}