Skip to content

Commit 5bfd02b

Browse files
committed
Add solution and test-cases for problem 986
1 parent a337ff8 commit 5bfd02b

File tree

4 files changed

+51
-27
lines changed

4 files changed

+51
-27
lines changed
30.7 KB
Loading

leetcode/901-1000/0986.Interval-List-Intersections/README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [986.Interval List Intersections][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two lists of closed intervals, `firstList` and `secondList`, where `firstList[i] = [starti, endi]` and `secondList[j] = [startj, endj]`. Each list of intervals is pairwise **disjoint** and in **sorted order**.
75

8-
**Example 1:**
6+
Return the intersection of these two interval lists.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
A **closed interval** `[a, b]` (with `a <= b`) denotes the set of real numbers `x` with `a <= x <= b`.
9+
10+
The **intersection** of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For example, the intersection of `[1, 3]` and `[2, 4]` is `[2, 3]`.
1411

15-
## 题意
16-
> ...
12+
**Example 1:**
1713

18-
## 题解
14+
![1](./1.png)
1915

20-
### 思路1
21-
> ...
22-
Interval List Intersections
23-
```go
2416
```
17+
Input: firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
18+
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
19+
```
20+
21+
**Example 2:**
2522

23+
```
24+
Input: firstList = [[1,3],[5,9]], secondList = []
25+
Output: []
26+
```
2627

2728
## 结语
2829

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(firstList [][]int, secondList [][]int) [][]int {
4+
la, lb := len(firstList), len(secondList)
5+
if la == 0 || lb == 0 {
6+
return nil
7+
}
8+
a, b := 0, 0
9+
ans := make([][]int, 0)
10+
for a < la && b < lb {
11+
af, as := firstList[a][0], firstList[a][1]
12+
bf, bs := secondList[b][0], secondList[b][1]
13+
if af > bs {
14+
b++
15+
continue
16+
}
17+
if bf > as {
18+
a++
19+
continue
20+
}
21+
ans = append(ans, []int{max(af, bf), min(as, bs)})
22+
if bs > as {
23+
a++
24+
} else {
25+
b++
26+
}
27+
}
28+
return ans
529
}

leetcode/901-1000/0986.Interval-List-Intersections/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
f, s [][]int
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{0, 2}, {5, 10}, {13, 23}, {24, 25}}, [][]int{{1, 5}, {8, 12}, {15, 24}, {25, 26}}, [][]int{{1, 2}, {5, 5}, {8, 10}, {15, 23}, {24, 24}, {25, 25}}},
17+
{"TestCase2", [][]int{{1, 3}, {5, 9}}, nil, nil},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.f, c.s)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.f, c.s)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)