diff --git a/leetcode/401-500/0436.Find-Right-Interval/README.md b/leetcode/401-500/0436.Find-Right-Interval/README.md index a42d5d2ae..e5f38f207 100644 --- a/leetcode/401-500/0436.Find-Right-Interval/README.md +++ b/leetcode/401-500/0436.Find-Right-Interval/README.md @@ -1,28 +1,38 @@ # [436.Find Right Interval][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 are given an array of `intervals`, where `intervals[i] = [starti, endi]` and each `starti` is **unique**. + +The **right interval** for an interval `i` is an interval `j` such that `startj >= endi` and `startj` is **minimized**. Note that `i` may equal `j`. + +Return an array of **right interval** indices for each interval `i`. If no **right interval** exists for interval `i`, then put `-1` at index `i`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: intervals = [[1,2]] +Output: [-1] +Explanation: There is only one interval in the collection, so it outputs -1. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Find Right Interval -```go ``` +Input: intervals = [[3,4],[2,3],[1,2]] +Output: [-1,0,1] +Explanation: There is no right interval for [3,4]. +The right interval for [2,3] is [3,4] since start0 = 3 is the smallest start that is >= end1 = 3. +The right interval for [1,2] is [2,3] since start1 = 2 is the smallest start that is >= end2 = 2. +``` + +**Example 3:** +``` +Input: intervals = [[1,4],[2,3],[3,4]] +Output: [-1,2,-1] +Explanation: There is no right interval for [1,4] and [3,4]. +The right interval for [2,3] is [3,4] since start2 = 3 is the smallest start that is >= end1 = 3. +``` ## 结语 diff --git a/leetcode/401-500/0436.Find-Right-Interval/Solution.go b/leetcode/401-500/0436.Find-Right-Interval/Solution.go index d115ccf5e..9596e7e5c 100644 --- a/leetcode/401-500/0436.Find-Right-Interval/Solution.go +++ b/leetcode/401-500/0436.Find-Right-Interval/Solution.go @@ -1,5 +1,61 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "fmt" + "sort" +) + +func Solution(intervals [][]int) []int { + l := len(intervals) + ans := make([]int, 0) + inCopy := make([][]int, l) + ai := make(map[string]int) + bi := make(map[string]int) + + for i := 0; i < l; i++ { + inCopy[i] = make([]int, 2) + copy(inCopy[i], intervals[i]) + k := fmt.Sprintf("%d-%d", intervals[i][0], intervals[i][1]) + ai[k] = i + } + + sort.Slice(inCopy, func(i, j int) bool { + if inCopy[i][0] == inCopy[j][0] { + return inCopy[i][1] < inCopy[j][1] + } + return inCopy[i][0] < inCopy[j][0] + }) + for i := 0; i < l; i++ { + k := fmt.Sprintf("%d-%d", inCopy[i][0], inCopy[i][1]) + bi[k] = i + } + + var bsearch func(int, int, int) int + bsearch = func(left, right, target int) int { + ll, r := left, right + for ll < r { + mid := ll + (r-ll)/2 + if inCopy[mid][0] < target { + ll = mid + 1 + continue + } + r = mid + } + if ll == right || r == left { + return -1 + } + k := fmt.Sprintf("%d-%d", inCopy[r][0], inCopy[r][1]) + return ai[k] + } + + for idx, in := range intervals { + k := fmt.Sprintf("%d-%d", in[0], in[1]) + if in[0] == in[1] { + ans = append(ans, idx) + continue + } + left := bi[k] + ans = append(ans, bsearch(left, l, in[1])) + } + return ans } diff --git a/leetcode/401-500/0436.Find-Right-Interval/Solution_test.go b/leetcode/401-500/0436.Find-Right-Interval/Solution_test.go index 14ff50eb4..4f622f219 100644 --- a/leetcode/401-500/0436.Find-Right-Interval/Solution_test.go +++ b/leetcode/401-500/0436.Find-Right-Interval/Solution_test.go @@ -10,12 +10,13 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs [][]int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{1, 2}}, []int{-1}}, + {"TestCase2", [][]int{{3, 4}, {2, 3}, {1, 2}}, []int{-1, 0, 1}}, + {"TestCase3", [][]int{{1, 4}, {2, 3}, {3, 4}}, []int{-1, 2, -1}}, + {"TestCase4", [][]int{{1, 1}, {3, 4}}, []int{0, -1}}, } // 开始测试 @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }