Skip to content

Add solution and test-cases for problem 2594 #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/README.md
Original file line number Diff line number Diff line change
@@ -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
30 changes: 28 additions & 2 deletions leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading