You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔥 3 Approaches 🔥 || Simple Fast and Easy || with Explanation
Solution - 1
import'dart:collection';
classSolution {
intminimumRounds(List<int> tasks) {
// Create a HashMap to store the count of each taskHashMap<int, int> getCount =HashMap();
// Iterate through the tasks array and store the count of each task// in the HashMapfor (int t in tasks) {
getCount[t] = (getCount[t] ??0) +1;
}
// Initialize a result variable to 0int result =0;
// Iterate through the values in the HashMap (which represent the counts of the tasks)for (int count in getCount.values) {
// If a task has a count of 1, it cannot be grouped with other tasks// and so we return -1if (count ==1) return-1;
// Add the number of rounds needed to process the tasks with count greater than or equal to 3
result += count ~/3;
// If there are any tasks left with count less than 3, we need an additional round// to process these tasksif (count %3!=0) result++;
}
// Return the total number of rounds needed to process all tasksreturn result;
}
}
Solution - 2
classSolution {
intminimumRounds(List<int> tasks) {
// Sort the tasks array in ascending order
tasks.sort();
int result =0, count =0;
// Iterate through each taskfor (int i =0; i < tasks.length; i++) {
// Increment the count of tasks with the same value
count++;
// If we have reached the last task or the current task is different from the next taskif (i == tasks.length -1|| tasks[i] != tasks[i +1]) {
// If there is only one task with this value, we cannot create groups of 3// and we return -1if (count ==1) {
return-1;
}
// Add the number of full groups of 3 we can create from the tasks with this value
result += count ~/3;
// If there are remaining tasks that cannot be included in a group of 3,// we need one more round to process these tasksif (count %3!=0) result++;
// Reset the count for the next set of tasks
count =0;
}
}
// Return the total number of rounds neededreturn result;
}
}
Solution - 3 GREEDY
import'dart:collection';
classSolution {
intminimumRounds(List<int> tasks) {
int ans =0;
HashMap<int, int> map =HashMap();
for (int number in tasks) {
map[number] = (map[number] ??0) +1;
}
for (int key in map.keys) {
int val = map[key] ??0;
if (val ==1) return-1;
int cnt = val ~/3;
if (val %3!=0) cnt++;
ans += cnt;
}
return ans;
}
}