-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #910 from 0xff-dev/826
Add solution and test-cases for problem 826
- Loading branch information
Showing
3 changed files
with
87 additions
and
27 deletions.
There are no files selected for viewing
31 changes: 17 additions & 14 deletions
31
leetcode/801-900/0826.Most-Profit-Assigning-Work/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 60 additions & 2 deletions
62
leetcode/801-900/0826.Most-Profit-Assigning-Work/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters