go: add «826. Most Profit Assigning Work»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-06-18 12:10:43 +02:00
parent e74d5bfa99
commit 8faeaa7e16
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,28 @@
package main
import (
"slices"
)
func maxProfitAssignment(difficulty []int, profit []int, worker []int) int {
maxAbility := slices.Max(worker)
jobs := make([]int, maxAbility+1)
for i, d := range difficulty {
if d > maxAbility {
continue
}
jobs[d] = max(jobs[d], profit[i])
}
for i := 1; i <= maxAbility; i++ {
jobs[i] = max(jobs[i], jobs[i-1])
}
total := 0
for _, ability := range worker {
total += jobs[ability]
}
return total
}