From dba89629329d1d82cb30d7ec6f0951cd0ba77d79 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 16 Mar 2025 17:10:46 +0800 Subject: [PATCH] Add solution and test-cases for problem 2594 --- .../README.md | 42 +++++++++++++++++++ .../Solution.go | 30 ++++++++++++- .../Solution_test.go | 20 ++++----- 3 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/README.md diff --git a/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/README.md b/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/README.md new file mode 100644 index 000000000..930375999 --- /dev/null +++ b/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/README.md @@ -0,0 +1,42 @@ +# [2594.Minimum Time to Repair Cars][title] + +## Description +You are given an integer array `ranks` representing the **ranks** of some mechanics. ranksi is the rank of the `ith` mechanic. A mechanic with a rank `r` can repair n cars in `r * n^2` minutes. + +You are also given an integer `cars` representing the total number of cars waiting in the garage to be repaired. + +Return the **minimum** time taken to repair all the cars. + +**Note**: All the mechanics can repair the cars simultaneously. + +**Example 1:** + +``` +Input: ranks = [4,2,3,1], cars = 10 +Output: 16 +Explanation: +- The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes. +- The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes. +- The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes. +- The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes. +It can be proved that the cars cannot be repaired in less than 16 minutes. +``` + +**Example 2:** + +``` +Input: ranks = [5,1,8], cars = 6 +Output: 16 +Explanation: +- The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes. +- The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes. +- The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes. +It can be proved that the cars cannot be repaired in less than 16 minutes. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/minimum-time-to-repair-cars +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/Solution.go b/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/Solution.go index d115ccf5e..28a28b6a4 100755 --- a/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/Solution.go +++ b/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/Solution.go @@ -1,5 +1,31 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "math" + "slices" +) + +func Solution(ranks []int, cars int) int64 { + var ok func(int64) bool + ok = func(minutes int64) bool { + c := int64(0) + for _, r := range ranks { + nn := minutes / int64(r) + i := int64(math.Sqrt(float64(nn))) + c += i + } + return c >= int64(cars) + } + + m := slices.Max(ranks) + l, r := int64(0), int64(m)*int64(cars)*int64(cars)+1 + for l < r { + m := (l + r) / 2 + if ok(m) { + r = m + continue + } + l = m + 1 + } + return l } diff --git a/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/Solution_test.go b/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/Solution_test.go index 14ff50eb4..1b9814916 100755 --- a/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/Solution_test.go +++ b/leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + ranks []int + cars int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{4, 3, 2, 1}, 10, 16}, + {"TestCase2", []int{5, 1, 8}, 6, 16}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.ranks, c.cars) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.ranks, c.cars) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }