Skip to content

Commit 8542b94

Browse files
committed
leetcode
1 parent 772db5c commit 8542b94

File tree

4 files changed

+320
-2
lines changed

4 files changed

+320
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// package main
2+
3+
// var mem = map[int]int{}
4+
5+
// func steps(n int) int {
6+
// if n <= 1 {
7+
// return -1
8+
// }
9+
// if ret, ok := mem[n]; ok {
10+
// return ret
11+
// }
12+
// if n == 2 {
13+
// return 1
14+
// }
15+
// if n%3 == 0 {
16+
// mem[n] = n / 3
17+
// return n / 3
18+
// }
19+
// out := 1 + steps(n-2)
20+
// mem[n] = out
21+
// return out
22+
// }
23+
24+
// func minimumRounds(tasks []int) int {
25+
// dic := map[int]int{}
26+
// for _, t := range tasks {
27+
// dic[t]++
28+
// }
29+
// out := 0
30+
// for _, n := range dic {
31+
// s := steps(n)
32+
// if s == -1 {
33+
// return -1
34+
// }
35+
// out += s
36+
// }
37+
// return out
38+
// }
39+
40+
package main
41+
42+
import "sort"
43+
44+
func minimumRounds(tasks []int) int {
45+
sort.Ints(tasks)
46+
var result int = 0
47+
for i := 0; i < len(tasks); i++ {
48+
var j int = i + 1
49+
for j < len(tasks) && tasks[j] == tasks[i] {
50+
j++
51+
if j == i+1 {
52+
return -1
53+
}
54+
}
55+
56+
result += (j - i + 2) / 3
57+
i = j
58+
}
59+
return result
60+
}
61+
62+
/*
63+
64+
65+
int minimumRounds(List<int> tasks) {
66+
tasks.sort();
67+
int res = 0;
68+
for (int i = 0; i < tasks.length;) {
69+
int j = i + 1;
70+
while (j < tasks.length && tasks[j] == tasks[i]) j++;
71+
if (j == i + 1) return -1;
72+
res += (j - i + 2) ~/ 3;
73+
i = j;
74+
}
75+
return res;
76+
}
77+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# 🔥 3 Approaches 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
import 'dart:collection';
7+
8+
class Solution {
9+
int minimumRounds(List<int> tasks) {
10+
// Create a HashMap to store the count of each task
11+
HashMap<int, int> getCount = HashMap();
12+
13+
// Iterate through the tasks array and store the count of each task
14+
// in the HashMap
15+
for (int t in tasks) {
16+
getCount[t] = (getCount[t] ?? 0) + 1;
17+
}
18+
19+
// Initialize a result variable to 0
20+
int result = 0;
21+
22+
// Iterate through the values in the HashMap (which represent the counts of the tasks)
23+
for (int count in getCount.values) {
24+
// If a task has a count of 1, it cannot be grouped with other tasks
25+
// and so we return -1
26+
if (count == 1) return -1;
27+
28+
// Add the number of rounds needed to process the tasks with count greater than or equal to 3
29+
result += count ~/ 3;
30+
31+
// If there are any tasks left with count less than 3, we need an additional round
32+
// to process these tasks
33+
if (count % 3 != 0) result++;
34+
}
35+
36+
// Return the total number of rounds needed to process all tasks
37+
return result;
38+
}
39+
}
40+
41+
```
42+
43+
## Solution - 2
44+
45+
```dart
46+
class Solution {
47+
int minimumRounds(List<int> tasks) {
48+
// Sort the tasks array in ascending order
49+
tasks.sort();
50+
int result = 0, count = 0;
51+
// Iterate through each task
52+
for (int i = 0; i < tasks.length; i++) {
53+
// Increment the count of tasks with the same value
54+
count++;
55+
// If we have reached the last task or the current task is different from the next task
56+
if (i == tasks.length - 1 || tasks[i] != tasks[i + 1]) {
57+
// If there is only one task with this value, we cannot create groups of 3
58+
// and we return -1
59+
if (count == 1) {
60+
return -1;
61+
}
62+
// Add the number of full groups of 3 we can create from the tasks with this value
63+
result += count ~/ 3;
64+
// If there are remaining tasks that cannot be included in a group of 3,
65+
// we need one more round to process these tasks
66+
if (count % 3 != 0) result++;
67+
// Reset the count for the next set of tasks
68+
count = 0;
69+
}
70+
}
71+
// Return the total number of rounds needed
72+
return result;
73+
}
74+
}
75+
```
76+
77+
## Solution - 3 GREEDY
78+
79+
```dart
80+
import 'dart:collection';
81+
82+
class Solution {
83+
int minimumRounds(List<int> tasks) {
84+
int ans = 0;
85+
HashMap<int, int> map = HashMap();
86+
for (int number in tasks) {
87+
map[number] = (map[number] ?? 0) + 1;
88+
}
89+
for (int key in map.keys) {
90+
int val = map[key] ?? 0;
91+
if (val == 1) return -1;
92+
int cnt = val ~/ 3;
93+
if (val % 3 != 0) cnt++;
94+
ans += cnt;
95+
}
96+
return ans;
97+
}
98+
}
99+
```

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ This repo contain leetcode solution using DART and GO programming language. Most
5353
- [**136.** Single Number](SingleNumber/single_number.dart)
5454
- [**19.** Remove Nth Node From End of List](RemoveNthNodeFromEndOfList/remove_nth_node_from_end_of_list.dart)
5555
- [**141.** Linked List Cycle](LinkedListCycle/linked_list_cycle.dart)
56-
- [**144.** Binary Tree Preorder Traversal](BinaryTreePreorderTraversal/binary_tree_preorder_traversal.dart)
57-
- [**145.** Binary Tree Postorder Traversal](BinaryTreePostorderTraversal/binary_tree_postorder_traversal.dart)
56+
- [**144.** Binary Tree PreOrder Traversal](BinaryTreePreorderTraversal/binary_tree_preorder_traversal.dart)
57+
- [**145.** Binary Tree PostOrder Traversal](BinaryTreePostorderTraversal/binary_tree_postorder_traversal.dart)
5858
- [**157.** Read N Characters Given Read4](ReadNCharactersGivenRead4/read_n_characters_given_read4.dart)
5959
- [**8160.** Intersection of Two Linked Lists](IntersectionOfTwoLinkedLists/intersection_of_two_linked_lists.dart)
6060
- [**163.** Missing Ranges](MissingRanges/missing_ranges.dart)
@@ -176,6 +176,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
176176
- [**980.** Unique Paths III](UniquePathsIII/unique_paths_iii.dart)
177177
- [**520.** Detect Capital](DetectCapital/detect_capital.dart)
178178
- [**944.** Delete Columns to Make Sorted](DeleteColumnsToMakeSorted/delete_columns_to_make_sorted.dart)
179+
- [**2244.** Minimum Rounds to Complete All Tasks](MinimumRoundsToCompleteAllTasks/minimum_rounds_to_complete_all_tasks.dart)
179180

180181
## Reach me via
181182

0 commit comments

Comments
 (0)