Skip to content

Commit b2703f4

Browse files
authored
Merge pull request #910 from 0xff-dev/826
Add solution and test-cases for problem 826
2 parents 266d70f + 5b83110 commit b2703f4

File tree

3 files changed

+87
-27
lines changed

3 files changed

+87
-27
lines changed

leetcode/801-900/0826.Most-Profit-Assigning-Work/README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [826.Most Profit Assigning Work][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+
You have `n` jobs and m workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
5+
6+
- `difficulty[i]` and `profit[i]` are the difficulty and the profit of the i<sup>th</sup> job, and
7+
- `worker[j]` is the ability of j<sup>th</sup> worker (i.e., the j<sup>th</sup> worker can only complete a job with difficulty at most `worker[j]`).
8+
9+
Every worker can be assigned **at most one job**, but one job can be **completed multiple times**.
10+
11+
- For example, if three workers attempt the same job that pays `$1`, then the total profit will be `$3`. If a worker cannot complete any job, their profit is `$0`.
12+
13+
Return the maximum profit we can achieve after assigning the workers to the jobs.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
19+
Output: 100
20+
Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get a profit of [20,20,30,30] separately.
1321
```
1422

15-
## 题意
16-
> ...
17-
18-
## 题解
23+
**Example 2:**
1924

20-
### 思路1
21-
> ...
22-
Most Profit Assigning Work
23-
```go
2425
```
25-
26+
Input: difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
27+
Output: 0
28+
```
2629

2730
## 结语
2831

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
type SegmentTreeNode struct {
9+
Left, Right, Max int
10+
LeftChild, RightChild *SegmentTreeNode
11+
}
12+
13+
func buildSegmentTree(nums []workProfit, left, right int) *SegmentTreeNode {
14+
if left == right {
15+
return &SegmentTreeNode{Left: left, Right: right, Max: nums[left].p}
16+
}
17+
18+
mid := (left + right) / 2
19+
leftNode := buildSegmentTree(nums, left, mid)
20+
rightNode := buildSegmentTree(nums, mid+1, right)
21+
max := max(leftNode.Max, rightNode.Max)
22+
23+
return &SegmentTreeNode{Left: left, Right: right, Max: max, LeftChild: leftNode, RightChild: rightNode}
24+
}
25+
26+
func queryMax(root *SegmentTreeNode, left, right int) int {
27+
if root.Left >= left && root.Right <= right {
28+
return root.Max
29+
}
30+
31+
if root.Right < left || root.Left > right {
32+
return math.MinInt32
33+
}
34+
35+
return max(queryMax(root.LeftChild, left, right), queryMax(root.RightChild, left, right))
36+
}
37+
38+
type workProfit struct {
39+
d, p int
40+
}
41+
42+
func Solution(difficulty []int, profit []int, worker []int) int {
43+
l := len(difficulty)
44+
dp := make([]workProfit, l)
45+
for i := 0; i < len(difficulty); i++ {
46+
dp[i] = workProfit{d: difficulty[i], p: profit[i]}
47+
}
48+
sort.Slice(dp, func(i, j int) bool {
49+
return dp[i].d < dp[j].d
50+
})
51+
52+
tree := buildSegmentTree(dp, 0, l-1)
53+
ans := 0
54+
for _, n := range worker {
55+
if idx := sort.Search(l, func(i int) bool {
56+
return dp[i].d > n
57+
}); idx != 0 {
58+
ans += queryMax(tree, 0, idx-1)
59+
}
60+
}
61+
62+
return ans
563
}

leetcode/801-900/0826.Most-Profit-Assigning-Work/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
d, p, w []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{2, 4, 6, 8, 10}, []int{10, 20, 30, 40, 50}, []int{4, 5, 6, 7}, 100},
17+
{"TestCase2", []int{85, 47, 57}, []int{24, 66, 99}, []int{40, 25, 25}, 0},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.d, c.p, c.w)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
26+
c.expect, got, c.d, c.p, c.w)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)