[LeetCode] 986. Interval List Intersections

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.

Return the intersection of these two interval lists.

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:

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: []

Example 3:

Input: firstList = [], secondList = [[4,8],[10,12]]
Output: []

Example 4:

Input: firstList = [[1,7]], secondList = [[3,10]]
Output: [[3,7]]

Constraints:

  • 0 <= firstList.length, secondList.length <= 1000
  • firstList.length + secondList.length >= 1
  • 0 <= starti < endi <= 109
  • endi < starti+1
  • 0 <= startj < endj <= 109
  • endj < startj+1

区间列表的交集。

给定两个由一些 闭区间 组成的列表,firstList 和 secondList ,其中 firstList[i] = [starti, endi] 而 secondList[j] = [startj, endj] 。每个区间列表都是成对 不相交 的,并且 已经排序 。

返回这 两个区间列表的交集 。

形式上,闭区间 [a, b](其中 a <= b)表示实数 x 的集合,而 a <= x <= b 。

两个闭区间的 交集 是一组实数,要么为空集,要么为闭区间。例如,[1, 3] 和 [2, 4] 的交集为 [2, 3] 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/interval-list-intersections
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意是给两组有序的 intervals of intervals,请将他们的重叠部分合并成一组。这个题目是偏扫描线那一类的题,但是具体做法是追击型的双指针。需要创建两个指针 i 和 j,分别记录 interval firstList 和 secondList 的下标。试图合并两边的 interval 的时候,依然采取 [start, end],start 两边取大,end 两边取小的原则(11,12行)。判断到底是 i++ 还是 j++ 是通过判断当前遍历到的 interval 谁的 end 小,谁小谁的指针就++(16 - 20行)。如果两边的 end 一样的,两边都往前走一步。

时间O(n)

空间O(1)

JavaScript实现

 1 /**
 2  * @param {number[][]} A
 3  * @param {number[][]} B
 4  * @return {number[][]}
 5  */
 6 var intervalIntersection = function (A, B) {
 7     let i = 0;
 8     let j = 0;
 9     let res = [];
10     while (i < A.length && j < B.length) {
11         let maxStart = Math.max(A[i][0], B[j][0]);
12         let minEnd = Math.min(A[i][1], B[j][1]);
13         if (maxStart <= minEnd) {
14             res.push([maxStart, minEnd]);
15         }
16         if (A[i][1] < B[j][1]) {
17             i++;
18         } else {
19             j++;
20         }
21     }
22     return res;
23 };

 

Java实现

 1 class Solution {
 2     public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
 3         List<int[]> res = new ArrayList<>();
 4         int i = 0;
 5         int j = 0;
 6         while (i < firstList.length && j < secondList.length) {
 7             int low = Math.max(firstList[i][0], secondList[j][0]);
 8             int high = Math.min(firstList[i][1], secondList[j][1]);
 9             if (low <= high) {
10                 res.add(new int[] {low, high});
11             }
12             if (firstList[i][1] < secondList[j][1]) {
13                 i++;
14             } else if (firstList[i][1] == secondList[j][1]) {
15                 i++;
16                 j++;
17             } else {
18                 j++;
19             }
20         }
21         return res.toArray(new int[res.size()][]);
22     }
23 }

 

扫描线相关题目

LeetCode 题目总结

posted @ 2020-03-05 03:47  CNoodle  阅读(296)  评论(0编辑  收藏  举报