|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* 2244. Minimum Rounds to Complete All Tasks *- |
| 5 | +
|
| 6 | +
|
| 7 | +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. |
| 8 | +
|
| 9 | +Return the minimum rounds required to complete all the tasks, or -1 if it is not possible to complete all the tasks. |
| 10 | +
|
| 11 | + |
| 12 | +
|
| 13 | +Example 1: |
| 14 | +
|
| 15 | +Input: tasks = [2,2,3,3,2,4,4,4,4,4] |
| 16 | +Output: 4 |
| 17 | +Explanation: To complete all the tasks, a possible plan is: |
| 18 | +- In the first round, you complete 3 tasks of difficulty level 2. |
| 19 | +- In the second round, you complete 2 tasks of difficulty level 3. |
| 20 | +- In the third round, you complete 3 tasks of difficulty level 4. |
| 21 | +- In the fourth round, you complete 2 tasks of difficulty level 4. |
| 22 | +It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4. |
| 23 | +Example 2: |
| 24 | +
|
| 25 | +Input: tasks = [2,3,3] |
| 26 | +Output: -1 |
| 27 | +Explanation: There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1. |
| 28 | + |
| 29 | +
|
| 30 | +Constraints: |
| 31 | +
|
| 32 | +1 <= tasks.length <= 105 |
| 33 | +1 <= tasks[i] <= 109 |
| 34 | +
|
| 35 | +*/ |
| 36 | + |
| 37 | +import 'dart:collection'; |
| 38 | + |
| 39 | +class A { |
| 40 | + int minimumRounds(List<int> tasks) { |
| 41 | + // Create a HashMap to store the count of each task |
| 42 | + HashMap<int, int> getCount = HashMap(); |
| 43 | + |
| 44 | + // Iterate through the tasks array and store the count of each task |
| 45 | + // in the HashMap |
| 46 | + for (int t in tasks) { |
| 47 | + getCount[t] = (getCount[t] ?? 0) + 1; |
| 48 | + } |
| 49 | + |
| 50 | + // Initialize a result variable to 0 |
| 51 | + int result = 0; |
| 52 | + |
| 53 | + // Iterate through the values in the HashMap (which represent the counts of the tasks) |
| 54 | + for (int count in getCount.values) { |
| 55 | + // If a task has a count of 1, it cannot be grouped with other tasks |
| 56 | + // and so we return -1 |
| 57 | + if (count == 1) return -1; |
| 58 | + |
| 59 | + // Add the number of rounds needed to process the tasks with count greater than or equal to 3 |
| 60 | + result += count ~/ 3; |
| 61 | + |
| 62 | + // If there are any tasks left with count less than 3, we need an additional round |
| 63 | + // to process these tasks |
| 64 | + if (count % 3 != 0) result++; |
| 65 | + } |
| 66 | + |
| 67 | + // Return the total number of rounds needed to process all tasks |
| 68 | + return result; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class B { |
| 73 | + int minimumRounds(List<int> tasks) { |
| 74 | + // Sort the tasks array in ascending order |
| 75 | + tasks.sort(); |
| 76 | + int result = 0, count = 0; |
| 77 | + // Iterate through each task |
| 78 | + for (int i = 0; i < tasks.length; i++) { |
| 79 | + // Increment the count of tasks with the same value |
| 80 | + count++; |
| 81 | + // If we have reached the last task or the current task is different from the next task |
| 82 | + if (i == tasks.length - 1 || tasks[i] != tasks[i + 1]) { |
| 83 | + // If there is only one task with this value, we cannot create groups of 3 |
| 84 | + // and we return -1 |
| 85 | + if (count == 1) { |
| 86 | + return -1; |
| 87 | + } |
| 88 | + // Add the number of full groups of 3 we can create from the tasks with this value |
| 89 | + result += count ~/ 3; |
| 90 | + // If there are remaining tasks that cannot be included in a group of 3, |
| 91 | + // we need one more round to process these tasks |
| 92 | + if (count % 3 != 0) result++; |
| 93 | + // Reset the count for the next set of tasks |
| 94 | + count = 0; |
| 95 | + } |
| 96 | + } |
| 97 | + // Return the total number of rounds needed |
| 98 | + return result; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +class C { |
| 103 | + int minimumRounds(List<int> tasks) { |
| 104 | + int ans = 0; |
| 105 | + HashMap<int, int> map = HashMap(); |
| 106 | + for (int number in tasks) { |
| 107 | + map[number] = (map[number] ?? 0) + 1; |
| 108 | + } |
| 109 | + for (int key in map.keys) { |
| 110 | + int val = map[key] ?? 0; |
| 111 | + if (val == 1) return -1; |
| 112 | + int cnt = val ~/ 3; |
| 113 | + if (val % 3 != 0) cnt++; |
| 114 | + ans += cnt; |
| 115 | + } |
| 116 | + return ans; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +class D { |
| 121 | + int minimumRounds(List<int> tasks) { |
| 122 | + tasks.sort(); |
| 123 | + int result = 0, count = 0; |
| 124 | + |
| 125 | + for (int i = 0; i < tasks.length; i++) { |
| 126 | + count++; |
| 127 | + |
| 128 | + if (i == tasks.length - 1 || tasks[i] != tasks[i + 1]) { |
| 129 | + if (count == 1) { |
| 130 | + return -1; |
| 131 | + } |
| 132 | + } |
| 133 | + result += count ~/ 3; |
| 134 | + if (count % 3 != 0) { |
| 135 | + result++; |
| 136 | + } |
| 137 | + count = 0; |
| 138 | + } |
| 139 | + return result; |
| 140 | + } |
| 141 | +} |
0 commit comments