-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
826. Most Profit Assigning Work.go
62 lines (53 loc) · 1.23 KB
/
826. Most Profit Assigning Work.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package leetcode
import (
"fmt"
"sort"
)
// Task define
type Task struct {
Difficulty int
Profit int
}
// Tasks define
type Tasks []Task
// Len define
func (p Tasks) Len() int { return len(p) }
// Swap define
func (p Tasks) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// SortByDiff define
type SortByDiff struct{ Tasks }
// Less define
func (p SortByDiff) Less(i, j int) bool {
return p.Tasks[i].Difficulty < p.Tasks[j].Difficulty
}
func maxProfitAssignment(difficulty []int, profit []int, worker []int) int {
if len(difficulty) == 0 || len(profit) == 0 || len(worker) == 0 {
return 0
}
tasks, res, index := []Task{}, 0, 0
for i := 0; i < len(difficulty); i++ {
tasks = append(tasks, Task{Difficulty: difficulty[i], Profit: profit[i]})
}
sort.Sort(SortByDiff{tasks})
sort.Ints(worker)
for i := 1; i < len(tasks); i++ {
tasks[i].Profit = max(tasks[i].Profit, tasks[i-1].Profit)
}
fmt.Printf("tasks = %v worker = %v\n", tasks, worker)
for _, w := range worker {
for index < len(difficulty) && w >= tasks[index].Difficulty {
index++
}
fmt.Printf("tasks【index】 = %v\n", tasks[index])
if index > 0 {
res += tasks[index-1].Profit
}
}
return res
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}