Skip to content

Commit

Permalink
Merge pull request #910 from 0xff-dev/826
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 826
  • Loading branch information
6boris authored Jun 30, 2024
2 parents 266d70f + 5b83110 commit b2703f4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 27 deletions.
31 changes: 17 additions & 14 deletions leetcode/801-900/0826.Most-Profit-Assigning-Work/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
# [826.Most Profit Assigning Work][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
You have `n` jobs and m workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:

- `difficulty[i]` and `profit[i]` are the difficulty and the profit of the i<sup>th</sup> job, and
- `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]`).

Every worker can be assigned **at most one job**, but one job can be **completed multiple times**.

- 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`.

Return the maximum profit we can achieve after assigning the workers to the jobs.

**Example 1:**

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

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Most Profit Assigning Work
```go
```

Input: difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
Output: 0
```

## 结语

Expand Down
62 changes: 60 additions & 2 deletions leetcode/801-900/0826.Most-Profit-Assigning-Work/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
package Solution

func Solution(x bool) bool {
return x
import (
"math"
"sort"
)

type SegmentTreeNode struct {
Left, Right, Max int
LeftChild, RightChild *SegmentTreeNode
}

func buildSegmentTree(nums []workProfit, left, right int) *SegmentTreeNode {
if left == right {
return &SegmentTreeNode{Left: left, Right: right, Max: nums[left].p}
}

mid := (left + right) / 2
leftNode := buildSegmentTree(nums, left, mid)
rightNode := buildSegmentTree(nums, mid+1, right)
max := max(leftNode.Max, rightNode.Max)

return &SegmentTreeNode{Left: left, Right: right, Max: max, LeftChild: leftNode, RightChild: rightNode}
}

func queryMax(root *SegmentTreeNode, left, right int) int {
if root.Left >= left && root.Right <= right {
return root.Max
}

if root.Right < left || root.Left > right {
return math.MinInt32
}

return max(queryMax(root.LeftChild, left, right), queryMax(root.RightChild, left, right))
}

type workProfit struct {
d, p int
}

func Solution(difficulty []int, profit []int, worker []int) int {
l := len(difficulty)
dp := make([]workProfit, l)
for i := 0; i < len(difficulty); i++ {
dp[i] = workProfit{d: difficulty[i], p: profit[i]}
}
sort.Slice(dp, func(i, j int) bool {
return dp[i].d < dp[j].d
})

tree := buildSegmentTree(dp, 0, l-1)
ans := 0
for _, n := range worker {
if idx := sort.Search(l, func(i int) bool {
return dp[i].d > n
}); idx != 0 {
ans += queryMax(tree, 0, idx-1)
}
}

return ans
}
21 changes: 10 additions & 11 deletions leetcode/801-900/0826.Most-Profit-Assigning-Work/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
d, p, w []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{2, 4, 6, 8, 10}, []int{10, 20, 30, 40, 50}, []int{4, 5, 6, 7}, 100},
{"TestCase2", []int{85, 47, 57}, []int{24, 66, 99}, []int{40, 25, 25}, 0},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.d, c.p, c.w)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.d, c.p, c.w)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit b2703f4

Please sign in to comment.