Skip to content

Commit afef753

Browse files
committed
Add solution and test-cases for problem 2895
1 parent a01e89c commit afef753

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [2895.Minimum Processing Time][title]
2+
3+
## Description
4+
You have a certain number of processors, each having 4 cores. The number of tasks to be executed is four times the number of processors. Each task must be assigned to a unique core, and each core can only be used once.
5+
6+
You are given an array `processorTime` representing the time each processor becomes available and an array `tasks` representing how long each task takes to complete. Return the minimum time needed to complete all tasks.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]
12+
13+
Output: 16
14+
15+
Explanation:
16+
17+
Assign the tasks at indices 4, 5, 6, 7 to the first processor which becomes available at time = 8, and the tasks at indices 0, 1, 2, 3 to the second processor which becomes available at time = 10.
18+
19+
The time taken by the first processor to finish the execution of all tasks is max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16.
20+
21+
The time taken by the second processor to finish the execution of all tasks is max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]
28+
29+
Output: 23
30+
31+
Explanation:
32+
33+
Assign the tasks at indices 1, 4, 5, 6 to the first processor and the others to the second processor.
34+
35+
The time taken by the first processor to finish the execution of all tasks is max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18.
36+
37+
The time taken by the second processor to finish the execution of all tasks is max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23.
38+
```
39+
40+
## 结语
41+
42+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
43+
44+
[title]: https://leetcode.com/problems/minimum-processing-time
45+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Solution
2+
3+
import "sort"
4+
5+
func Solution(processorTime []int, tasks []int) int {
6+
sort.Slice(tasks, func(i, j int) bool {
7+
return tasks[i] > tasks[j]
8+
})
9+
sort.Ints(processorTime)
10+
ans := 0
11+
index := 0
12+
for _, n := range processorTime {
13+
mm := max(tasks[index], tasks[index+1], tasks[index+2], tasks[index+3])
14+
index += 4
15+
ans = max(ans, n+mm)
16+
}
17+
return ans
18+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
processorTime, tasks []int
14+
expect int
15+
}{
16+
{"TestCase1", []int{8, 10}, []int{2, 2, 3, 1, 8, 7, 4, 5}, 16},
17+
{"TestCase2", []int{10, 20}, []int{2, 3, 1, 2, 5, 8, 4, 3}, 23},
18+
}
19+
20+
// 开始测试
21+
for i, c := range cases {
22+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
23+
got := Solution(c.processorTime, c.tasks)
24+
if !reflect.DeepEqual(got, c.expect) {
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.processorTime, c.tasks)
27+
}
28+
})
29+
}
30+
}
31+
32+
// 压力测试
33+
func BenchmarkSolution(b *testing.B) {
34+
}
35+
36+
// 使用案列
37+
func ExampleSolution() {
38+
}

0 commit comments

Comments
 (0)