📜  使任何间隔等于给定集合的并集所需的最小移除量

📅  最后修改于: 2021-05-18 00:48:45             🧑  作者: Mango

给定大小为N ( 1≤N≤1e5 )的集合S ,该集合S由间隔组成,任务是找到需要从集合中删除的最小间隔,以使其余间隔中的任何一个都等于该集合的并集。

例子:

方法:该问题可以基于以下观察得到解决:

请按照以下步骤解决问题:

  1. 遍历给定的时间间隔集。
  2. 对于集合中的每个间隔,找到其左边界大于或等于其左边界以及右边界小于或等于其右边界的所有间隔。将此类间隔的计数存储在变量中,例如Count
  3. 找到每个间隔的所有N – Count (因为N – Count会给出删除的间隔数)值的最小值。
  4. 打印获得的最小值作为所需答案。

下面是上述方法的实现:

C++
// C++ implementaion of the above approach
 
#include 
using namespace std;
 
// Function to count minimum number of removals
// required to make an interval equal to the
// union of the given Set
int findMinDeletions(vector >& v,
                     int n)
{
    // Stores the minimum number of removals
    int minDel = INT_MAX;
 
    // Traverse the Set
    for (int i = 0; i < n; i++) {
 
        // Left Boundary
        int L = v[i].first;
 
        // Right Boundary
        int R = v[i].second;
 
        // Stores count of intervals
        // lying within current interval
        int Count = 0;
 
        // Traverse over all remaining intervals
        for (int j = 0; j < n; j++) {
 
            // Check if interval lies within
            // the current interval
            if (v[j].first >= L && v[j].second <= R) {
 
                // Increase count
                Count += 1;
            }
        }
 
        // Update minimum removals required
        minDel = min(minDel, n - Count);
    }
    return minDel;
}
 
// Driver Code
int main()
{
    vector > v;
    v.push_back({ 1, 3 });
    v.push_back({ 4, 12 });
    v.push_back({ 5, 8 });
    v.push_back({ 13, 20 });
 
    int N = v.size();
 
    // Returns the minimum removals
    cout << findMinDeletions(v, N);
 
    return 0;
}


Java
// Java implementaion of the above approach
import java.util.*;
class GFG{
 
// Function to count minimum number of removals
// required to make an interval equal to the
// union of the given Set
static int findMinDeletions(int [][]v,
                            int n)
{
   
    // Stores the minimum number of removals
    int minDel = Integer.MAX_VALUE;
 
    // Traverse the Set
    for (int i = 0; i < n; i++)
    {
 
        // Left Boundary
        int L = v[i][0];
 
        // Right Boundary
        int R = v[i][1];
 
        // Stores count of intervals
        // lying within current interval
        int Count = 0;
 
        // Traverse over all remaining intervals
        for (int j = 0; j < n; j++)
        {
 
            // Check if interval lies within
            // the current interval
            if (v[j][0] >= L && v[j][1] <= R)
            {
 
                // Increase count
                Count += 1;
            }
        }
 
        // Update minimum removals required
        minDel = Math.min(minDel, n - Count);
    }
    return minDel;
}
 
// Driver Code
public static void main(String[] args)
{
    int [][]v = {{ 1, 3 },
                 { 4, 12 },
                 { 5, 8 },
                 { 13, 20 }};
 
    int N = v.length;
 
    // Returns the minimum removals
    System.out.print(findMinDeletions(v, N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementaion of the above approach
 
 
# Function to count minimum number of removals
# required to make an interval equal to the
# union of the given Set
def findMinDeletions(v, n):
   
    # Stores the minimum number of removals
    minDel = 10**18
 
    # Traverse the Set
    for i in range(n):
 
        # Left Boundary
        L = v[i][0]
 
        # Right Boundary
        R = v[i][1]
 
        # Stores count of intervals
        # lying within current interval
        Count = 0
 
        # Traverse over all remaining intervals
        for j in range(n):
 
            # Check if interval lies within
            # the current interval
            if (v[j][1] >= L and v[j][0] <= R):
                 
                # Increase count
                Count += 1
 
        # Update minimum removals required
        minDel = min(minDel, n - Count)
    return minDel
 
# Driver Code
if __name__ == '__main__':
    v = []
    v.append([1, 3])
    v.append([4, 12])
    v.append([5, 8])
    v.append([13, 2])
 
    N = len(v)
 
    # Returns the minimum removals
    print (findMinDeletions(v, N))
 
# This code is contributed by mohit kumar 29


C#
// C# implementaion of the above approach
using System;
 
public class GFG{
 
// Function to count minimum number of removals
// required to make an interval equal to the
// union of the given Set
static int findMinDeletions(int [,]v,
                            int n)
{
   
    // Stores the minimum number of removals
    int minDel = int.MaxValue;
 
    // Traverse the Set
    for (int i = 0; i < n; i++)
    {
 
        // Left Boundary
        int L = v[i,0];
 
        // Right Boundary
        int R = v[i,1];
 
        // Stores count of intervals
        // lying within current interval
        int Count = 0;
 
        // Traverse over all remaining intervals
        for (int j = 0; j < n; j++)
        {
 
            // Check if interval lies within
            // the current interval
            if (v[j,0] >= L && v[j,1] <= R)
            {
 
                // Increase count
                Count += 1;
            }
        }
 
        // Update minimum removals required
        minDel = Math.Min(minDel, n - Count);
    }
    return minDel;
}
 
// Driver Code
public static void Main(String[] args)
{
    int [,]v = {{ 1, 3 },
                 { 4, 12 },
                 { 5, 8 },
                 { 13, 20 }};
 
    int N = v.GetLength(0);
 
    // Returns the minimum removals
    Console.Write(findMinDeletions(v, N));
}
}
 
  
 
// This code is contributed by 29AjayKumar


输出:
2

时间复杂度: O(N 2 )
辅助空间: O(1)