LeetCode //C - 436. Find Right Interval

436. Find Right Interval

You are given an array of 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] and each starti is unique.

The right interval for an interval i is an interval j such that s t a r t j > = e n d i start_j >= end_i startj>=endi and startj is minimized. Note that i may equal j.

Return an array of right interval indices for each interval i. If no right interval exists for interval i, then put -1 at index i.
 

Example 1:

Input: intervals = [[1,2]]
Output: [-1]
Explanation: There is only one interval in the collection, so it outputs -1.

Example 2:

Input: intervals = [[3,4],[2,3],[1,2]]
Output: [-1,0,1]
Explanation: There is no right interval for [3,4].
The right interval for [2,3] is [3,4] since start0 = 3 is the smallest start that is >= end1 = 3.
The right interval for [1,2] is [2,3] since start1 = 2 is the smallest start that is >= end2 = 2.

Example 3:

Input: intervals = [[1,4],[2,3],[3,4]]
Output: [-1,2,-1]
Explanation: There is no right interval for [1,4] and [3,4].
The right interval for [2,3] is [3,4] since start2 = 3 is the smallest start that is >= end1 = 3.

Constraints:
  • 1 < = i n t e r v a l s . l e n g t h < = 2 ∗ 1 0 4 1 <= intervals.length <= 2 * 10^4 1<=intervals.length<=2104
  • intervals[i].length == 2
  • − 1 0 6 < = s t a r t i < = e n d i < = 1 0 6 -10^6 <= starti <= endi <= 10^6 106<=starti<=endi<=106
  • The start point of each interval is unique.

From: LeetCode
Link: 436. Find Right Interval


Solution:

Ideas:
  1. Create an array to store the original indices of the intervals since we’ll sort the intervals based on their start times but still need to return the indices based on the original input order.
  2. Sort the intervals based on their start times while keeping track of their original indices.
  3. For each interval, use binary search to find the smallest interval whose start time is greater than or equal to the current interval’s end time.
  4. Populate the result array with the indices found in step 3. If no such interval is found, put -1 for that interval.
  5. Return the result array.
Code:
int compare(const void* a, const void* b) {
    int* intervalA = *(int**)a;
    int* intervalB = *(int**)b;
    return intervalA[0] - intervalB[0];
}

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* findRightInterval(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize) {
    // Create an array to store the original index of each interval
    int** intervalsWithIndex = (int**)malloc(intervalsSize * sizeof(int*));
    for (int i = 0; i < intervalsSize; i++) {
        intervalsWithIndex[i] = (int*)malloc(3 * sizeof(int)); // Increase size to store original index
        intervalsWithIndex[i][0] = intervals[i][0]; // start
        intervalsWithIndex[i][1] = intervals[i][1]; // end
        intervalsWithIndex[i][2] = i; // original index
    }
    
    // Sort the intervals by their start time
    qsort(intervalsWithIndex, intervalsSize, sizeof(int*), compare);
    
    // Allocate memory for the result array
    *returnSize = intervalsSize;
    int* result = (int*)malloc(intervalsSize * sizeof(int));
    
    // Binary search to find the right interval for each interval
    for (int i = 0; i < intervalsSize; i++) {
        int left = 0, right = intervalsSize - 1;
        int target = intervals[i][1];
        int found = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (intervalsWithIndex[mid][0] >= target) {
                found = intervalsWithIndex[mid][2];
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        result[i] = found;
    }
    
    // Free the allocated memory
    for (int i = 0; i < intervalsSize; i++) {
        free(intervalsWithIndex[i]);
    }
    free(intervalsWithIndex);
    
    return result;
}

相关推荐

  1. AFM 433

    2024-04-02 02:18:04       33 阅读
  2. LeetCode //C - 436. Find Right Interval

    2024-04-02 02:18:04       34 阅读
  3. trino-435:dynamic catalog

    2024-04-02 02:18:04       56 阅读
  4. 439 - Knight Moves (UVA)

    2024-04-02 02:18:04       71 阅读
  5. leetcode 437 路径总和

    2024-04-02 02:18:04       58 阅读
  6. 435. 无重叠区间

    2024-04-02 02:18:04       42 阅读
  7. Knight Moves(UVA 439

    2024-04-02 02:18:04       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-02 02:18:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-02 02:18:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-02 02:18:04       82 阅读
  4. Python语言-面向对象

    2024-04-02 02:18:04       91 阅读

热门阅读

  1. Windows下配深度学习环境

    2024-04-02 02:18:04       37 阅读
  2. docker入门

    2024-04-02 02:18:04       32 阅读
  3. python中线程与协程

    2024-04-02 02:18:04       35 阅读
  4. 微信小程序中实现埋点的方法

    2024-04-02 02:18:04       40 阅读
  5. Azure入门实践-如何创建两个虚拟网络的对等连接

    2024-04-02 02:18:04       38 阅读
  6. C++ 学习10大网站推荐(Bjarne Stroustrup)

    2024-04-02 02:18:04       32 阅读
  7. 二分查找算法刷题记录 -LC34

    2024-04-02 02:18:04       36 阅读
  8. Linux 服务service(一)

    2024-04-02 02:18:04       32 阅读
  9. Nginx: proxy_set_header 与 add_header 区别

    2024-04-02 02:18:04       42 阅读