Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ca04225

Browse files
committed
Merge pull request #395 from 0xff-dev/2244
Add solution and test-cases for problem 2244
2 parents 2ada18f + e500eba commit ca04225

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

‎leetcode/2201-2300/2244.Minimum-Rounds-to-Complete-All-Tasks/README.md‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [2244.Minimum Rounds to Complete All Tasks][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
Return the **minimum** rounds required to complete all the tasks, or -1 if it is not possible to complete all the tasks.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: tasks = [2,2,3,3,2,4,4,4,4,4]
12+
Output: 4
13+
Explanation: To complete all the tasks, a possible plan is:
14+
- In the first round, you complete 3 tasks of difficulty level 2.
15+
- In the second round, you complete 2 tasks of difficulty level 3.
16+
- In the third round, you complete 3 tasks of difficulty level 4.
17+
- In the fourth round, you complete 2 tasks of difficulty level 4.
18+
It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.
1319
```
1420

15-
## 题意
16-
> ...
17-
18-
## 题解
21+
**Example 2:**
1922

20-
### 思路1
21-
> ...
22-
Minimum Rounds to Complete All Tasks
23-
```go
2423
```
25-
24+
Input: tasks = [2,3,3]
25+
Output: -1
26+
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.
27+
```
2628

2729
## 结语
2830

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(tasks []int) int {
4+
taskLevel := make(map[int]int)
5+
for _, t := range tasks {
6+
taskLevel[t]++
7+
}
8+
rounds := 0
9+
10+
for _, count := range taskLevel {
11+
if count == 1 {
12+
return -1
13+
}
14+
cond := count % 3
15+
if cond == 0 {
16+
rounds += count / 3
17+
continue
18+
}
19+
// 4(2+2), 7(3+2+2), 10(3+3+2+2), 13(3+3+3+2+2)
20+
if cond == 1 {
21+
rounds += (count-4)/3 + 2
22+
continue
23+
}
24+
// 5(3+2), 8(3+3+2), 11(3+3+3+2)
25+
rounds += count/3 + 1
26+
}
27+
return rounds
528
}

‎leetcode/2201-2300/2244.Minimum-Rounds-to-Complete-All-Tasks/Solution_test.go‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{2, 2, 3, 3, 2, 4, 4, 4, 4, 4}, 4},
17+
{"TestCase2", []int{2, 3, 3}, -1},
18+
{"TestCase3", []int{1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7}, 12},
19+
{"TestCase4", []int{119, 115, 115, 119, 118, 113, 118, 120, 110, 113, 119, 115, 116, 118, 120, 117, 116, 111, 113, 119, 115, 113, 115, 111, 112, 119, 111, 111, 110, 112, 113, 120, 110, 111, 112, 111, 119, 112, 113, 112, 115, 116, 113, 114, 118, 119, 115, 114, 114, 112, 110, 117, 120, 110, 117, 116, 120, 118, 110, 120, 119, 113, 119, 120, 113, 110, 120, 114, 119, 115, 119, 117, 120, 116, 113, 113, 110, 118, 117, 116, 114, 114, 111, 116, 119, 112, 113, 116, 112, 116, 119, 112, 114, 114, 112, 118, 116, 113, 117, 116}, 38},
1920
}
2021

2122
// 开始测试
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

33-
//压力测试
34+
//压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
//使用案列
38+
//使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /