From afef753f6a065f1081fdce7a36786247768d142f Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 8 May 2025 09:03:59 +0800 Subject: [PATCH] Add solution and test-cases for problem 2895 --- .../2895.Minimum-Processing-Time/README.md | 45 +++++++++++++++++++ .../2895.Minimum-Processing-Time/Solution.go | 18 ++++++++ .../Solution_test.go | 38 ++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 leetcode/2801-2900/2895.Minimum-Processing-Time/README.md create mode 100755 leetcode/2801-2900/2895.Minimum-Processing-Time/Solution.go create mode 100755 leetcode/2801-2900/2895.Minimum-Processing-Time/Solution_test.go diff --git a/leetcode/2801-2900/2895.Minimum-Processing-Time/README.md b/leetcode/2801-2900/2895.Minimum-Processing-Time/README.md new file mode 100644 index 00000000..5ce1b315 --- /dev/null +++ b/leetcode/2801-2900/2895.Minimum-Processing-Time/README.md @@ -0,0 +1,45 @@ +# [2895.Minimum Processing Time][title] + +## Description +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. + +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. + +**Example 1:** + +``` +Input: processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5] + +Output: 16 + +Explanation: + +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. + +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. + +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. +``` + +**Example 2:** + +``` +Input: processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3] + +Output: 23 + +Explanation: + +Assign the tasks at indices 1, 4, 5, 6 to the first processor and the others to the second processor. + +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. + +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. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/minimum-processing-time +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2801-2900/2895.Minimum-Processing-Time/Solution.go b/leetcode/2801-2900/2895.Minimum-Processing-Time/Solution.go new file mode 100755 index 00000000..ac1b8174 --- /dev/null +++ b/leetcode/2801-2900/2895.Minimum-Processing-Time/Solution.go @@ -0,0 +1,18 @@ +package Solution + +import "sort" + +func Solution(processorTime []int, tasks []int) int { + sort.Slice(tasks, func(i, j int) bool { + return tasks[i] > tasks[j] + }) + sort.Ints(processorTime) + ans := 0 + index := 0 + for _, n := range processorTime { + mm := max(tasks[index], tasks[index+1], tasks[index+2], tasks[index+3]) + index += 4 + ans = max(ans, n+mm) + } + return ans +} diff --git a/leetcode/2801-2900/2895.Minimum-Processing-Time/Solution_test.go b/leetcode/2801-2900/2895.Minimum-Processing-Time/Solution_test.go new file mode 100755 index 00000000..ff18943b --- /dev/null +++ b/leetcode/2801-2900/2895.Minimum-Processing-Time/Solution_test.go @@ -0,0 +1,38 @@ +package Solution + +import ( + "reflect" + "strconv" + "testing" +) + +func TestSolution(t *testing.T) { + // 测试用例 + cases := []struct { + name string + processorTime, tasks []int + expect int + }{ + {"TestCase1", []int{8, 10}, []int{2, 2, 3, 1, 8, 7, 4, 5}, 16}, + {"TestCase2", []int{10, 20}, []int{2, 3, 1, 2, 5, 8, 4, 3}, 23}, + } + + // 开始测试 + for i, c := range cases { + t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { + got := Solution(c.processorTime, c.tasks) + if !reflect.DeepEqual(got, c.expect) { + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.processorTime, c.tasks) + } + }) + } +} + +// 压力测试 +func BenchmarkSolution(b *testing.B) { +} + +// 使用案列 +func ExampleSolution() { +}