LeetCode //C - 757. Set Intersection Size At Least Two

757. Set Intersection Size At Least Two

You are given a 2D integer array intervals where i n t e r v a l s [ i ] = [ s t a r t i , e n d i ] intervals[i] = [start_i, end_i] intervals[i]=[starti,endi] represents all the integers from s t a r t i start_i starti to e n d i end_i endi inclusively.

A containing set is an array nums where each interval from intervals has at least two integers in nums.

  • For example, if intervals = [[1,3], [3,7], [8,9]], then [1,2,4,7,8,9] and [2,3,4,8,9] are containing sets.

Return the minimum possible size of a containing set.

Example 1:

Input: intervals = [[1,3],[3,7],[8,9]]
Output: 5
Explanation: let nums = [2, 3, 4, 8, 9].

It can be shown that there cannot be any containing array of size 4.

Example 2:

Input: intervals = [[1,3],[1,4],[2,5],[3,5]]
Output: 3
Explanation: let nums = [2, 3, 4].

It can be shown that there cannot be any containing array of size 2.

Example 3:

Input: intervals = [[1,2],[2,3],[2,4],[4,5]]
Output: 5
Explanation: let nums = [1, 2, 3, 4, 5].

It can be shown that there cannot be any containing array of size 4.

Constraints:
  • 1 <= intervals.length <= 3000
  • intervals[i].length == 2
  • 0 < = s t a r t i < e n d i < = 10 8 0 <= start_i < end_i <= 10^8 0<=starti<endi<=108

From: LeetCode

Link: 757. Set Intersection Size At Least Two


Solution:

Ideas:
  • Sort intervals by end point (ascending), then by start point (descending) when end points are equal

  • Track state using two variables: point1 and point2 (last two points added to our set)

  • Process each interval and check how many of our current points it contains:

    • Contains 0 points (start > point2): Add 2 points at positions end-1 and end
    • Contains 1 point (start > point1): Add 1 point at position end
    • Contains 2+ points: Add 0 points
Code:
c 复制代码
// Comparator function to sort intervals by end point
// If end points are equal, sort by start point in descending order
int compare(const void* a, const void* b) {
    int* intervalA = *(int**)a;
    int* intervalB = *(int**)b;
    if (intervalA[1] != intervalB[1]) {
        return intervalA[1] - intervalB[1];  // Sort by end point ascending
    }
    return intervalB[0] - intervalA[0];  // If end points equal, sort by start point descending
}

int intersectionSizeTwo(int** intervals, int intervalsSize, int* intervalsColSize) {
    if (intervalsSize == 0) return 0;
    
    // Sort intervals by end point
    qsort(intervals, intervalsSize, sizeof(int*), compare);
    
    int count = 0;
    int point1 = INT_MIN;  // Second to last point added
    int point2 = INT_MIN;  // Last point added
    
    for (int i = 0; i < intervalsSize; i++) {
        int start = intervals[i][0];
        int end = intervals[i][1];
        
        if (start > point2) {
            // Current interval doesn't contain any of our points
            // Add the two rightmost points of this interval
            count += 2;
            point1 = end - 1;
            point2 = end;
        } else if (start > point1) {
            // Current interval contains only point2
            // Add one more point (the rightmost one)
            count += 1;
            point1 = point2;
            point2 = end;
        }
        // If start <= point1, then the interval contains both point1 and point2
        // No need to add any points
    }
    
    return count;
}
相关推荐
用户6120414922131 分钟前
C语言做的区块链模拟系统(极简版)
c语言·后端·敏捷开发
weixin_307779131 分钟前
C++进程监视器与自动启动程序
开发语言·c++·算法
草莓熊Lotso21 分钟前
【C语言强化训练16天】--从基础到进阶的蜕变之旅:Day12
c语言·开发语言·c++·刷题
CoovallyAIHub1 小时前
目标检测模型评估金标准:mAP全解读,Coovally助你高效调参!
深度学习·算法·计算机视觉
whitepure1 小时前
万字详解常用算法(Java版)
java·后端·算法
CoovallyAIHub1 小时前
基于视觉的果园无人机导航:一种基于干预模仿学习与VAE控制器的真实世界验证
深度学习·算法·计算机视觉
励志五个月成为嵌入式糕手1 小时前
0820 SQlite与c语言的结合
c语言·oracle·sqlite
尘世闲鱼2 小时前
移动零【三种思路】
c++·leetcode
初学小刘3 小时前
线性回归:机器学习中的基石
算法·机器学习·线性回归
2501_9248895511 小时前
商超高峰客流统计误差↓75%!陌讯多模态融合算法在智慧零售的实战解析
大数据·人工智能·算法·计算机视觉·零售