给定一个由{L, R}形式的N 个范围组成的数组arr[] ,任务是找到最大化范围的数量,使得每个范围可以由该范围中的任何整数唯一表示。
例子:
Input: arr[] = {{1, 2}, {2, 3}, {3, 4}}
Output: 3
Explanation:
Number of ranges can be maximized by following representations:
- Range {1, 2} can be represented by 1.
- Range {2, 3} can be represented by 2.
- Range {3, 4} can be represented by 3.
Therefore, the maximized count of ranges is 3.
Input: arr[] = {{1, 4}, {4, 4}, {2, 2}, {3, 4}, {1, 1}}
Output: 4
朴素的方法:解决给定问题的最简单的方法是对范围进行排序并从L的最小值到R的最大值进行迭代,以贪婪地为特定范围分配一个整数,并保留可能分配的整数的计数。完成上述步骤后,打印得到的计数作为结果。
时间复杂度: O(M * N),其中M是给定范围中的最大元素。
辅助空间: O(1)
高效方法:上述方法也可以通过使用贪婪方法进行优化,其思想是通过按递增顺序选择给定范围来分配每个范围中的最小唯一值。请按照以下步骤解决问题:
- 按升序对给定的范围数组进行排序。
- 初始化一个向量,比如prev为arr[] ,它存储先前分配给给定范围的值。
- 初始化一个变量,比如count为1以存储最大范围数,其中每个范围可以由范围中的整数唯一表示。
- 在[1, N – 1]范围内遍历给定数组 arr[ ]并执行以下步骤:
- 初始化一个向量,比如current[]作为arr[i] ,它存储分配给当前范围的值。
- 如果max(current[i][1], prev[i][1]) – max(current[i][0], prev[i][0] – 1) 的值为正,则更新值prev为{1 + max(current[i][0], prev[i][0] – 1), max(current[i][1], prev[i][1])}并增加值计数为1 。
- 否则,检查下一个范围。
- 完成以上步骤后,打印count的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum number
// of ranges where each range can be
// uniquely represented by an integer
int maxRanges(vector > arr,
int N)
{
// Sort the ranges in ascending order
sort(arr.begin(), arr.end());
// Stores the count of ranges
int count = 1;
// Stores previously assigned range
vector prev = arr[0];
// Traverse the vector arr[]
for (int i = 1; i < N; i++) {
vector last = arr[i - 1];
vector current = arr[i];
// Skip the similar ranges
// of size 1
if (last[0] == current[0]
&& last[1] == current[1]
&& current[1] == current[0])
continue;
// Find the range of integer
// available to be assigned
int start = max(prev[0], current[0] - 1);
int end = max(prev[1], current[1]);
// Check if an integer is
// available to be assigned
if (end - start > 0) {
// Update the previously
// assigned range
prev[0] = 1 + start;
prev[1] = end;
// Update the count of range
count++;
}
}
// Return the maximum count of
// ranges
return count;
}
// Driver Code
int main()
{
vector > range = {
{ 1, 4 }, { 4, 4 },
{ 2, 2 }, { 3, 4 },
{ 1, 1 }
};
int N = range.size();
cout << maxRanges(range, N);
return 0;
}
Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
// Function to find the maximum number
// of ranges where each range can be
// uniquely represented by an integer
static int maxRanges(Integer arr[][], int N)
{
// Sort the ranges in ascending order
Arrays.sort(arr, (a, b) -> {
if (a[0].equals(b[0]))
return Integer.compare(a[1], b[1]);
return Integer.compare(a[0], b[0]);
});
// Stores the count of ranges
int count = 1;
// Stores previously assigned range
Integer prev[] = arr[0];
// Traverse the vector arr[]
for (int i = 1; i < N; i++) {
Integer last[] = arr[i - 1];
Integer current[] = arr[i];
// Skip the similar ranges
// of size 1
if (last[0] == current[0]
&& last[1] == current[1]
&& current[1] == current[0])
continue;
// Find the range of integer
// available to be assigned
int start = Math.max(prev[0], current[0] - 1);
int end = Math.max(prev[1], current[1]);
// Check if an integer is
// available to be assigned
if (end - start > 0) {
// Update the previously
// assigned range
prev[0] = 1 + start;
prev[1] = end;
// Update the count of range
count++;
}
}
// Return the maximum count of
// ranges
return count;
}
// Driver Code
public static void main(String[] args)
{
Integer range[][] = {
{ 1, 4 }, { 4, 4 }, { 2, 2 }, { 3, 4 }, { 1, 1 }
};
int N = range.length;
System.out.print(maxRanges(range, N));
}
}
// This code is contributed by Kingash.
Python3
# Python 3 program for the above approach
# Function to find the maximum number
# of ranges where each range can be
# uniquely represented by an integer
def maxRanges(arr, N):
# Sort the ranges in ascending order
arr.sort()
# Stores the count of ranges
count = 1
# Stores previously assigned range
prev = arr[0]
# Traverse the vector arr[]
for i in range(1, N):
last = arr[i - 1]
current = arr[i]
# Skip the similar ranges
# of size 1
if (last[0] == current[0]
and last[1] == current[1]
and current[1] == current[0]):
continue
# Find the range of integer
# available to be assigned
start = max(prev[0], current[0] - 1)
end = max(prev[1], current[1])
# Check if an integer is
# available to be assigned
if (end - start > 0):
# Update the previously
# assigned range
prev[0] = 1 + start
prev[1] = end
# Update the count of range
count += 1
# Return the maximum count of
# ranges
return count
# Driver Code
if __name__ == "__main__":
arr = [
[1, 4], [4, 4],
[2, 2], [3, 4],
[1, 1]]
N = len(arr)
print(maxRanges(arr, N))
# This code is contributed by ukasp.
Javascript
输出:
4
时间复杂度: O(N * log N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live