📜  要插入为间隔[0,R]的可用非重叠间隔的计数

📅  最后修改于: 2021-05-04 13:28:18             🧑  作者: Mango

给定一个整数R ,它表示范围[0,R] ,两个数组start []end []的大小为N ,表示范围在[0,R]中的开始和结束时间间隔。任务是计算需要插入数组中的可用非重叠间隔的数量,以便在合并数组start []end []中的各个范围时,合并的间隔变为[0,R]。

例子:

方法:想法是使用排序来解决问题。

  • 最初,对两个给定的数组进行排序,以使使用的间隔合并在一起。
  • 现在,以一种排序的方式迭代数组(即,一个指针将指向start的起点,而另一个指针将指向end的起点)。
  • 如果当前开始元素较小,则表示正在寻找一个新的间隔;如果当前结束索引较小,则表明正在退出当前的间隔。
  • 在此过程中,将存储当前活动间隔的计数。如果在任何时候当前活动计数为0,则存在一个可用间隔。因此,可用间隔的计数增加。
  • 这样,两个数组都被迭代。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
  
#include 
using namespace std;
  
// The following two functions
// are used to sort the array.
// QuickSort is being implemented in
// the following functions
  
// Function to find the pivot index
int partiton(int arr[], int l, int h)
{
    int pivot = arr[l];
    int i = l + 1;
    int j = h;
  
    while (i <= j) {
  
        while (i <= h
               && arr[i] < pivot) {
            i++;
        }
  
        while (j > l
               && arr[j] > pivot) {
            j--;
        }
  
        if (i < j) {
  
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
        else
            i++;
    }
  
    arr[l] = arr[j];
    arr[j] = pivot;
    return j;
}
  
// Function to implement Quick Sort
void sortArray(int arr[], int l, int h)
{
    if (l >= h)
        return;
  
    // Pivot is pointing to pivot index
    // before which every element
    // is smaller and after pivot,
    // every element is greater
    int pivot = partiton(arr, l, h);
  
    // Sort the array before pivot element
    sortArray(arr, l, pivot - 1);
  
    // Sort the array after pivot element
    sortArray(arr, pivot + 1, h);
}
  
// Function to count the available intervals
// from the given range of numbers
int findMaxIntervals(int start[], int end[],
                     int n, int R)
{
    int ans = 0;
    int prev = 0;
    int currActive = 0;
    int i = 0;
    int j = 0;
  
    // If range starts after 0
    // then an interval is available
    // from 0 to start[0]
    if (start[0] > 0)
        ans++;
  
    while (i < n && j < n) {
        // When a new interval starts
        if (start[i] < end[j]) {
  
            // Since index variable i is being
            // incremented, the current active
            // interval will also get incremented
            i++;
            currActive++;
        }
  
        // When the current interval ends
        else if (start[i] > end[j]) {
  
            // Since index variable j is being
            // decremented, the currect active
            // interval will also get decremented
            j++;
            currActive--;
        }
  
        // When start and end both are same
        // there is no change in currActive
        else {
            i++;
            j++;
        }
        if (currActive == 0) {
            ans++;
        }
    }
  
    // If the end of interval
    // is before the range
    // so interval is available
    // at the end
    if (end[n - 1] < R)
        ans++;
    return ans;
}
  
// Driver code
int main()
{
    int R, N;
    R = 10;
    N = 3;
    int start[N] = { 2, 5, 8 };
    int end[N] = { 3, 9, 10 };
  
    // Sort the start array
    sortArray(start, 0, N - 1);
  
    // Sort the end array
    sortArray(end, 0, N - 1);
  
    // Calling the function
    cout << findMaxIntervals(
        start, end, N, R);
}


Java
// Java implementation of the above approach
import java.util.*;
class GFG{
  
// The following two functions
// are used to sort the array.
// QuickSort is being implemented in
// the following functions
  
// Function to find the pivot index
static int partiton(int arr[], int l, int h)
{
    int pivot = arr[l];
    int i = l + 1;
    int j = h;
  
    while (i <= j) 
    {
        while (i <= h && arr[i] < pivot) 
        {
            i++;
        }
  
        while (j > l && arr[j] > pivot) 
        {
            j--;
        }
  
        if (i < j) 
        {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
        else
            i++;
    }
  
    arr[l] = arr[j];
    arr[j] = pivot;
    return j;
}
  
// Function to implement Quick Sort
static void sortArray(int arr[], int l, int h)
{
    if (l >= h)
        return;
  
    // Pivot is pointing to pivot index
    // before which every element
    // is smaller and after pivot,
    // every element is greater
    int pivot = partiton(arr, l, h);
  
    // Sort the array before pivot element
    sortArray(arr, l, pivot - 1);
  
    // Sort the array after pivot element
    sortArray(arr, pivot + 1, h);
}
  
// Function to count the available intervals
// from the given range of numbers
static int findMaxIntervals(int start[], 
                            int end[],
                            int n, int R)
{
    int ans = 0;
    int prev = 0;
    int currActive = 0;
    int i = 0;
    int j = 0;
  
    // If range starts after 0
    // then an interval is available
    // from 0 to start[0]
    if (start[0] > 0)
        ans++;
  
    while (i < n && j < n) 
    {
        // When a new interval starts
        if (start[i] < end[j])
        {
  
            // Since index variable i is being
            // incremented, the current active
            // interval will also get incremented
            i++;
            currActive++;
        }
  
        // When the current interval ends
        else if (start[i] > end[j]) 
        {
  
            // Since index variable j is being
            // decremented, the currect active
            // interval will also get decremented
            j++;
            currActive--;
        }
  
        // When start and end both are same
        // there is no change in currActive
        else
        {
            i++;
            j++;
        }
        if (currActive == 0) 
        {
            ans++;
        }
    }
  
    // If the end of interval
    // is before the range
    // so interval is available
    // at the end
    if (end[n - 1] < R)
        ans++;
    return ans;
}
  
// Driver code
public static void main(String args[])
{
    int R, N;
    R = 10;
    N = 3;
    int start[] = new int[]{ 2, 5, 8 };
    int end[] = new int[]{ 3, 9, 10 };
  
    // Sort the start array
    sortArray(start, 0, N - 1);
  
    // Sort the end array
    sortArray(end, 0, N - 1);
  
    // Calling the function
    System.out.print(findMaxIntervals(start, end, N, R));
}
}
  
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the above approach
  
# The following two functions
# are used to sort the array.
# QuickSort is being implemented in
# the following functions
  
# Function to find the pivot index
def partiton(arr, l, h):
      
    pivot = arr[l]
    i = l + 1
    j = h
  
    while (i <= j):
        while (i <= h and arr[i] < pivot):
            i += 1
  
        while (j > l and arr[j] > pivot):
            j -= 1
  
        if (i < j):
            temp = arr[i]
            arr[i] = arr[j]
            arr[j] = temp
            i += 1
            j -= 1
        else:
            i += 1
  
    arr[l] = arr[j]
    arr[j] = pivot
    return j
  
# Function to implement Quick Sort
def sortArray(arr, l, h):
      
    if (l >= h):
        return
  
    # Pivot is pointing to pivot index
    # before which every element
    # is smaller and after pivot,
    # every element is greater
    pivot = partiton(arr, l, h)
  
    # Sort the array before pivot element
    sortArray(arr, l, pivot - 1)
  
    # Sort the array after pivot element
    sortArray(arr, pivot + 1, h)
  
# Function to count the available intervals
# from the given range of numbers
def findMaxIntervals(start, end, n, R):
      
    ans = 0
    prev = 0
    currActive = 0
    i = 0
    j = 0
  
    # If range starts after 0
    # then an interval is available
    # from 0 to start[0]
    if (start[0] > 0):
        ans += 1
  
    while (i < n and j < n):
          
        # When a new interval starts
        if (start[i] < end[j]):
              
            # Since index variable i is being
            # incremented, the current active
            # interval will also get incremented
            i += 1
            currActive += 1
  
        # When the current interval ends
        elif (start[i] > end[j]):
              
            # Since index variable j is being
            # decremented, the currect active
            # interval will also get decremented
            j += 1
            currActive -= 1
  
        # When start and end both are same
        # there is no change in currActive
        else:
            i += 1
            j += 1
        if (currActive == 0):
            ans += 1
  
    # If the end of interval
    # is before the range
    # so interval is available
    # at the end
    if (end[n - 1] < R):
        ans += 1
    return ans
  
# Driver code
if __name__ == '__main__':
      
    R = 10
    N = 3
    start = [ 2, 5, 8 ]
    end = [ 3, 9, 10 ]
  
    # Sort the start array
    sortArray(start, 0, N - 1)
  
    # Sort the end array
    sortArray(end, 0, N - 1)
  
    # Calling the function
    print(findMaxIntervals(start, end, N, R))
  
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the above approach
using System;
class GFG{
  
// The following two functions
// are used to sort the array.
// QuickSort is being implemented in
// the following functions
  
// Function to find the pivot index
static int partiton(int []arr, int l, int h)
{
    int pivot = arr[l];
    int i = l + 1;
    int j = h;
  
    while (i <= j) 
    {
        while (i <= h && arr[i] < pivot) 
        {
            i++;
        }
  
        while (j > l && arr[j] > pivot) 
        {
            j--;
        }
  
        if (i < j) 
        {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
        else
            i++;
    }
  
    arr[l] = arr[j];
    arr[j] = pivot;
    return j;
}
  
// Function to implement Quick Sort
static void sortArray(int []arr, int l, int h)
{
    if (l >= h)
        return;
  
    // Pivot is pointing to pivot index
    // before which every element
    // is smaller and after pivot,
    // every element is greater
    int pivot = partiton(arr, l, h);
  
    // Sort the array before pivot element
    sortArray(arr, l, pivot - 1);
  
    // Sort the array after pivot element
    sortArray(arr, pivot + 1, h);
}
  
// Function to count the available intervals
// from the given range of numbers
static int findMaxIntervals(int []start, 
                            int []end,
                            int n, int R)
{
    int ans = 0;
    int prev = 0;
    int currActive = 0;
    int i = 0;
    int j = 0;
  
    // If range starts after 0
    // then an interval is available
    // from 0 to start[0]
    if (start[0] > 0)
        ans++;
  
    while (i < n && j < n) 
    {
        // When a new interval starts
        if (start[i] < end[j])
        {
  
            // Since index variable i is being
            // incremented, the current active
            // interval will also get incremented
            i++;
            currActive++;
        }
  
        // When the current interval ends
        else if (start[i] > end[j]) 
        {
  
            // Since index variable j is being
            // decremented, the currect active
            // interval will also get decremented
            j++;
            currActive--;
        }
  
        // When start and end both are same
        // there is no change in currActive
        else 
        {
            i++;
            j++;
        }
        if (currActive == 0) 
        {
            ans++;
        }
    }
  
    // If the end of interval
    // is before the range
    // so interval is available
    // at the end
    if (end[n - 1] < R)
        ans++;
    return ans;
}
  
// Driver code
public static void Main()
{
    int R, N;
    R = 10;
    N = 3;
    int []start = new int[]{ 2, 5, 8 };
    int []end = new int[]{ 3, 9, 10 };
  
    // Sort the start array
    sortArray(start, 0, N - 1);
  
    // Sort the end array
    sortArray(end, 0, N - 1);
  
    // Calling the function
    Console.Write(findMaxIntervals(start, end, N, R));
}
}
  
// This code is contributed by Code_Mech


输出:
2