Skip to content

Commit dba8962

Browse files
committed
Add solution and test-cases for problem 2594
1 parent f0a9eb1 commit dba8962

File tree

3 files changed

+80
-12
lines changed

3 files changed

+80
-12
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [2594.Minimum Time to Repair Cars][title]
2+
3+
## Description
4+
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.
5+
6+
You are also given an integer `cars` representing the total number of cars waiting in the garage to be repaired.
7+
8+
Return the **minimum** time taken to repair all the cars.
9+
10+
**Note**: All the mechanics can repair the cars simultaneously.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: ranks = [4,2,3,1], cars = 10
16+
Output: 16
17+
Explanation:
18+
- The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes.
19+
- The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes.
20+
- The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes.
21+
- The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
22+
It can be proved that the cars cannot be repaired in less than 16 minutes.
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: ranks = [5,1,8], cars = 6
29+
Output: 16
30+
Explanation:
31+
- The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes.
32+
- The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
33+
- The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes.
34+
It can be proved that the cars cannot be repaired in less than 16 minutes.
35+
```
36+
37+
## 结语
38+
39+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
40+
41+
[title]: https://leetcode.com/problems/minimum-time-to-repair-cars
42+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"math"
5+
"slices"
6+
)
7+
8+
func Solution(ranks []int, cars int) int64 {
9+
var ok func(int64) bool
10+
ok = func(minutes int64) bool {
11+
c := int64(0)
12+
for _, r := range ranks {
13+
nn := minutes / int64(r)
14+
i := int64(math.Sqrt(float64(nn)))
15+
c += i
16+
}
17+
return c >= int64(cars)
18+
}
19+
20+
m := slices.Max(ranks)
21+
l, r := int64(0), int64(m)*int64(cars)*int64(cars)+1
22+
for l < r {
23+
m := (l + r) / 2
24+
if ok(m) {
25+
r = m
26+
continue
27+
}
28+
l = m + 1
29+
}
30+
return l
531
}

leetcode/2501-2600/2594.Minimum-Time-to-Repair-Cars/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
ranks []int
14+
cars int
15+
expect int64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{4, 3, 2, 1}, 10, 16},
18+
{"TestCase2", []int{5, 1, 8}, 6, 16},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.ranks, c.cars)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.ranks, c.cars)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)