Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 436 #621

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions leetcode/401-500/0436.Find-Right-Interval/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
60 changes: 58 additions & 2 deletions leetcode/401-500/0436.Find-Right-Interval/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 8 additions & 7 deletions leetcode/401-500/0436.Find-Right-Interval/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
}

// 开始测试
Expand All @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
}
}

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

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