-
Notifications
You must be signed in to change notification settings - Fork 186
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 #1055 from 0xff-dev/2054
Add solution and test-cases for problem 2054
- Loading branch information
Showing
6 changed files
with
88 additions
and
22 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 26 additions & 13 deletions
39
leetcode/2001-2100/2054.Two-Best-Non-Overlapping-Events/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
57 changes: 55 additions & 2 deletions
57
leetcode/2001-2100/2054.Two-Best-Non-Overlapping-Events/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,58 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
import "sort" | ||
|
||
type SegmentTreeNode2054 struct { | ||
Left, Right, Max int | ||
LeftChild, RightChild *SegmentTreeNode2054 | ||
} | ||
|
||
func buildSegmentTree2054(nums [][]int, left, right int) *SegmentTreeNode2054 { | ||
if left == right { | ||
return &SegmentTreeNode2054{Left: left, Right: right, Max: nums[left][2]} | ||
} | ||
|
||
mid := (left + right) / 2 | ||
leftNode := buildSegmentTree2054(nums, left, mid) | ||
rightNode := buildSegmentTree2054(nums, mid+1, right) | ||
max := max(leftNode.Max, rightNode.Max) | ||
|
||
return &SegmentTreeNode2054{Left: left, Right: right, Max: max, LeftChild: leftNode, RightChild: rightNode} | ||
} | ||
|
||
func queryMax2054(root *SegmentTreeNode2054, left, right int) int { | ||
if root.Left >= left && root.Right <= right { | ||
return root.Max | ||
} | ||
|
||
if root.Right < left || root.Left > right { | ||
return -1 | ||
} | ||
|
||
return max(queryMax2054(root.LeftChild, left, right), queryMax2054(root.RightChild, left, right)) | ||
} | ||
|
||
func Solution(events [][]int) int { | ||
sort.Slice(events, func(i, j int) bool { | ||
a, b := events[i], events[j] | ||
if a[0] == b[0] { | ||
return a[1] < b[1] | ||
} | ||
return a[0] < b[0] | ||
}) | ||
l := len(events) | ||
|
||
tree := buildSegmentTree2054(events, 0, l-1) | ||
ans := 0 | ||
for i, e := range events { | ||
ans = max(ans, e[2]) | ||
idx := sort.Search(l-i-1, func(ii int) bool { | ||
return events[i+1+ii][0] > e[1] | ||
}) | ||
if idx == l-i-1 { | ||
continue | ||
} | ||
ans = max(ans, e[2]+queryMax2054(tree, i+1+idx, l-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