986. Interval List Intersections

You are given two lists of closed intervals, firstList and secondList, where f i r s t L i s t [ i ] = [ s t a r t i , e n d i ] firstList[i] = [start_i, end_i] firstList[i]=[starti,endi] and s e c o n d L i s t [ j ] = [ s t a r t j , e n d j ] secondList[j] = [start_j, end_j] 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: []

Constraints:
  • 0 <= firstList.length, secondList.length <= 1000
  • firstList.length + secondList.length >= 1
  • 0 < = s t a r t i < e n d i < = 10 9 0 <= start_i < end_i <= 10^9 0<=starti<endi<=109
  • e n d i < s t a r t i + 1 end_i < start_{i+1} endi<starti+1
  • 0 < = s t a r t j < e n d j < = 10 9 0 <= start_j < end_j <= 10^9 0<=startj<endj<=109
  • e n d j < s t a r t j + 1 end_j < start_{j+1} endj<startj+1

From: LeetCode
Link: 986. Interval List Intersections


Solution:

Ideas:
  • Use two pointers, one for each interval list.

  • Since lists are sorted and disjoint, scan them only once.

  • For current intervals:

    • Intersection start = max(start1, start2)
    • Intersection end = min(end1, end2)
  • If start <= end, the intervals overlap → add this range to the answer.

  • Move forward the pointer of the interval that ends first.

  • Continue until one list is fully processed.

Code:
static inline int max2(int a, int b) { return a > b ? a : b; }
static inline int min2(int a, int b) { return a < b ? a : b; }

int** intervalIntersection(
    int** firstList, int firstListSize, int* firstListColSize,
    int** secondList, int secondListSize, int* secondListColSize,
    int* returnSize, int** returnColumnSizes
) {
    (void)firstListColSize;   // always 2 per problem statement
    (void)secondListColSize;  // always 2 per problem statement

    *returnSize = 0;

    // If one list is empty, answer is empty
    if (firstListSize == 0 || secondListSize == 0) {
        *returnColumnSizes = NULL;
        return NULL;
    }

    // Max possible intersections is at most firstListSize + secondListSize
    int cap = firstListSize + secondListSize;
    int** res = (int**)malloc(sizeof(int*) * cap);
    int* colSizes = (int*)malloc(sizeof(int) * cap);

    int i = 0, j = 0;

    while (i < firstListSize && j < secondListSize) {
        int aStart = firstList[i][0], aEnd = firstList[i][1];
        int bStart = secondList[j][0], bEnd = secondList[j][1];

        int start = max2(aStart, bStart);
        int end   = min2(aEnd, bEnd);

        if (start <= end) {
            int* pair = (int*)malloc(sizeof(int) * 2);
            pair[0] = start;
            pair[1] = end;
            res[*returnSize] = pair;
            colSizes[*returnSize] = 2;
            (*returnSize)++;
        }

        // Move the one that ends first
        if (aEnd < bEnd) i++;
        else j++;
    }

    // Optional: shrink to actual size
    if (*returnSize == 0) {
        free(res);
        free(colSizes);
        *returnColumnSizes = NULL;
        return NULL;
    }

    res = (int**)realloc(res, sizeof(int*) * (*returnSize));
    colSizes = (int*)realloc(colSizes, sizeof(int) * (*returnSize));

    *returnColumnSizes = colSizes;
    return res;
}
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐