diff --git a/leetcode/901-1000/0986.Interval-List-Intersections/1.png b/leetcode/901-1000/0986.Interval-List-Intersections/1.png new file mode 100644 index 000000000..72102be2e Binary files /dev/null and b/leetcode/901-1000/0986.Interval-List-Intersections/1.png differ diff --git a/leetcode/901-1000/0986.Interval-List-Intersections/README.md b/leetcode/901-1000/0986.Interval-List-Intersections/README.md index 832e30d99..40b4ea909 100644 --- a/leetcode/901-1000/0986.Interval-List-Intersections/README.md +++ b/leetcode/901-1000/0986.Interval-List-Intersections/README.md @@ -1,28 +1,29 @@ # [986.Interval List Intersections][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 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**. -**Example 1:** +Return the intersection of these two interval lists. -``` -Input: a = "11", b = "1" -Output: "100" -``` +A **closed interval** `[a, b]` (with `a <= b`) denotes the set of real numbers `x` with `a <= x <= b`. + +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]`. -## 题意 -> ... +**Example 1:** -## 题解 +![1](./1.png) -### 思路1 -> ... -Interval List Intersections -```go ``` +Input: firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]] +Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]] +``` + +**Example 2:** +``` +Input: firstList = [[1,3],[5,9]], secondList = [] +Output: [] +``` ## 结语 diff --git a/leetcode/901-1000/0986.Interval-List-Intersections/Solution.go b/leetcode/901-1000/0986.Interval-List-Intersections/Solution.go index d115ccf5e..0cc9153f6 100644 --- a/leetcode/901-1000/0986.Interval-List-Intersections/Solution.go +++ b/leetcode/901-1000/0986.Interval-List-Intersections/Solution.go @@ -1,5 +1,29 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(firstList [][]int, secondList [][]int) [][]int { + la, lb := len(firstList), len(secondList) + if la == 0 || lb == 0 { + return nil + } + a, b := 0, 0 + ans := make([][]int, 0) + for a < la && b < lb { + af, as := firstList[a][0], firstList[a][1] + bf, bs := secondList[b][0], secondList[b][1] + if af > bs { + b++ + continue + } + if bf > as { + a++ + continue + } + ans = append(ans, []int{max(af, bf), min(as, bs)}) + if bs > as { + a++ + } else { + b++ + } + } + return ans } diff --git a/leetcode/901-1000/0986.Interval-List-Intersections/Solution_test.go b/leetcode/901-1000/0986.Interval-List-Intersections/Solution_test.go index 14ff50eb4..29e4ed61e 100644 --- a/leetcode/901-1000/0986.Interval-List-Intersections/Solution_test.go +++ b/leetcode/901-1000/0986.Interval-List-Intersections/Solution_test.go @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + f, s [][]int + expect [][]int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"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}}}, + {"TestCase2", [][]int{{1, 3}, {5, 9}}, nil, nil}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.f, c.s) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.f, c.s) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }