826. 安排工作以达到最大收益

安排工作以达到最大收益

解法一: 排序

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
func maxProfitAssignment(difficulty []int, profit []int, worker []int) int {
type work struct {
d int
p int
}
n := len(difficulty)
arr := make([]work, n)
for i := 0; i < n; i++ {
arr[i] = work{
d: difficulty[i],
p: profit[i],
}
}
sort.Slice(arr, func(i, j int) bool {
return arr[i].d <= arr[j].d
})
sort.Ints(worker)
ans := 0
i := 0
maxVal := 0
for _, w := range worker {
for i < n && w >= arr[i].d {
maxVal = max(maxVal, arr[i].p)
i++
}
ans += maxVal
}
return ans
}
作者

wuhunyu

发布于

2024-05-17

更新于

2025-01-15

许可协议