给定一个由区间组成的大小为N ( 1 ≤ N ≤ 1e5 ) 的集合S ,任务是找到需要从集合中移除的最小区间,使得剩余的区间中的任何一个变得等于该集合的并集。
例子:
Input: S = {[1, 3], [4, 12], [5, 8], [13, 20]}
Output: 2
Explanation:
Removing the intervals [1, 3] and [13, 20] modifies the set to { [4, 12], [5, 8]}. The interval [4, 12] is the union of the set.
Input : S = {[1, 2], [1, 10], [4, 8], [3, 7]}
Output: 0
Explanation:
Union of the given set = {[1, 10]}, which is already present in the set. Therefore, no removals required.
方法:该问题可以基于以下观察来解决:
Observation: To make any interval equal to the union of the set, the set must contain an interval [L, R] such that all the remaining intervals have their left boundary greater than equal to L and right boundary less than equal to R.
请按照以下步骤解决问题:
- 遍历给定的一组区间。
- 对于集合中的每个区间,找出其左边界大于或等于其左边界以及其右边界小于或等于其右边界的所有区间。将此类间隔的计数存储在一个变量中,例如Count 。
- 找到每个间隔的所有N – Count (因为N – Count将给出删除的间隔数)值的最小值。
- 打印获得的最小值作为所需答案。
以下是上述方法的实现:
C++
// C++ implementation 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 implementation 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 implementation 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# implementation 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
Javascript
2
时间复杂度: O(N 2 )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live