Skip to content

Commit 26341bd

Browse files
committed
Add solution and test-cases for problem 857
1 parent 09a1dcb commit 26341bd

File tree

3 files changed

+91
-25
lines changed

3 files changed

+91
-25
lines changed

leetcode/801-900/0857.Minimum-Cost-to-Hire-K-Workers/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [857.Minimum Cost to Hire K Workers][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There are `n` workers. You are given two integer arrays `quality` and `wage` where `quality[i]` is the quality of the `ith` worker and `wage[i]` is the minimum wage expectation for the `ith` worker.
5+
6+
We want to hire exactly `k` workers to form a **paid group**. To hire a group of `k` workers, we must pay them according to the following rules:
7+
8+
1. Every worker in the paid group must be paid at least their minimum wage expectation.
9+
2. In the group, each worker's pay must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.
10+
11+
Given the integer `k`, return the least amount of money needed to form a paid group satisfying the above conditions. Answers within `10^-5` of the actual answer will be accepted.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: quality = [10,20,5], wage = [70,50,30], k = 2
17+
Output: 105.00000
18+
Explanation: We pay 70 to 0th worker and 35 to 2nd worker.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Minimum Cost to Hire K Workers
23-
```go
2423
```
25-
24+
Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
25+
Output: 30.66667
26+
Explanation: We pay 4 to 0th worker, 13.33333 to 2nd and 3rd workers separately.
27+
```
2628

2729
## 结语
2830

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,69 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import (
4+
"container/heap"
5+
"math"
6+
"sort"
7+
)
8+
9+
type Pair struct {
10+
wageRatio float64
11+
quality int
12+
}
13+
14+
type PairList []Pair
15+
16+
func (p PairList) Len() int { return len(p) }
17+
func (p PairList) Less(i, j int) bool {
18+
return p[i].wageRatio < p[j].wageRatio
19+
}
20+
func (p PairList) Swap(i, j int) {
21+
p[i], p[j] = p[j], p[i]
22+
}
23+
24+
type MaxHeap []int
25+
26+
func (h MaxHeap) Len() int { return len(h) }
27+
func (h MaxHeap) Less(i, j int) bool { return h[i] > h[j] }
28+
func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
29+
func (h *MaxHeap) Push(x interface{}) {
30+
*h = append(*h, x.(int))
31+
}
32+
func (h *MaxHeap) Pop() interface{} {
33+
old := *h
34+
n := len(old)
35+
x := old[n-1]
36+
*h = old[0 : n-1]
437
return x
538
}
39+
40+
func Solution(quality []int, wage []int, k int) float64 {
41+
n := len(quality)
42+
totalCost := float64(1<<63 - 1)
43+
currentTotalQuality := 0.0
44+
wageToQualityRatio := make(PairList, n)
45+
46+
for i := 0; i < n; i++ {
47+
wageToQualityRatio[i] = Pair{float64(wage[i]) / float64(quality[i]), quality[i]}
48+
}
49+
50+
sort.Sort(wageToQualityRatio)
51+
52+
highestQualityWorkers := &MaxHeap{}
53+
heap.Init(highestQualityWorkers)
54+
55+
for i := 0; i < n; i++ {
56+
heap.Push(highestQualityWorkers, wageToQualityRatio[i].quality)
57+
currentTotalQuality += float64(wageToQualityRatio[i].quality)
58+
59+
if highestQualityWorkers.Len() > k {
60+
currentTotalQuality -= float64(heap.Pop(highestQualityWorkers).(int))
61+
}
62+
63+
if highestQualityWorkers.Len() == k {
64+
totalCost = math.Min(totalCost, currentTotalQuality*wageToQualityRatio[i].wageRatio)
65+
}
66+
}
67+
68+
return totalCost
69+
}

leetcode/801-900/0857.Minimum-Cost-to-Hire-K-Workers/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+
q, w []int
14+
k int
15+
expect float64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{10, 20, 5}, []int{70, 50, 30}, 2, 105.0000},
18+
{"TestCase2", []int{3, 1, 10, 10, 1}, []int{4, 8, 2, 2, 7}, 3, 30.666666666666664},
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.q, c.w, c.k)
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 %v",
27+
c.expect, got, c.q, c.w, c.k)
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)