Most Profit Assigning Work Solutions in Go
Number 826
Difficulty Medium
Acceptance 38.6%
Link LeetCode
Other languages —
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodeimport ("fmt""sort")// Task definetype Task struct {Difficulty intProfit int}// Tasks definetype Tasks []Task// Len definefunc (p Tasks) Len() int { return len(p) }// Swap definefunc (p Tasks) Swap(i, j int) { p[i], p[j] = p[j], p[i] }// SortByDiff definetype SortByDiff struct{ Tasks }// Less definefunc (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, 0for 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}