给定 N 个 LR 范围。任务是打印在给定范围内出现次数最多的数字。
注: 1 <= L <= R <= 10 6
例子:
Input: range[] = { {1, 6}, {2, 3}, {2, 5}, {3, 8} }
Output: 3
1 occurs in 1 range {1, 6}
2 occurs 3 in 3 range {1, 6}, {2, 3}, {2, 5}
3 occurs 4 in 4 range {1, 6}, {2, 3}, {2, 5}, {3, 8}
4 occurs 3 in 3 range {1, 6}, {2, 5}, {3, 8}
5 occurs 3 in 3 range {1, 6}, {2, 5}, {3, 8}
6 occurs 2 in 2 range {1, 6}, {3, 8}
7 occurs 1 in 1 range {3, 8}
8 occurs 1 in 1 range {3, 8}
Input: range[] = { {1, 4}, {1, 9}, {1, 2}};
Output: 1
方法:该方法类似于 n 范围内出现的最大整数。唯一不同的是找到范围的下限和上限。这样就不需要从 1 遍历到 MAX。
下面是解决这个问题的分步算法:
- 用 0 初始化一个 freq 数组,让数组的大小为 10^6,因为这是可能的最大值。
- 对于给定范围的每个起始索引,将freq[l] 增加 1 。
- 对于给定范围的每个结束索引,将freq[r+1] 减少 1 。
- 从最小 L 迭代到最大 R 并通过freq[i] += freq[i-1]添加频率。
- 具有最大值 freq[i] 的索引将是答案。
- 存储索引并返回它。
下面是上述方法的实现:
C++
// C++ program to check the most occurring
// element in given range
#include
using namespace std;
// Function that returns the maximum element.
int maxOccurring(int range[][2], int n)
{
// freq array to store the frequency
int freq[(int)(1e6 + 2)] = { 0 };
int first = 0, last = 0;
// iterate and mark the hash array
for (int i = 0; i < n; i++) {
int l = range[i][0];
int r = range[i][1];
// increase the hash array by 1 at L
freq[l] += 1;
// Decrease the hash array by 1 at R
freq[r + 1] -= 1;
first = min(first, l);
last = max(last, r);
}
// stores the maximum frequency
int maximum = 0;
int element = 0;
// check for the most occurring element
for (int i = first; i <= last; i++) {
// increase the frequency
freq[i] = freq[i - 1] + freq[i];
// check if is more than the previous one
if (freq[i] > maximum) {
maximum = freq[i];
element = i;
}
}
return element;
}
// Driver code
int main()
{
int range[][2] = { { 1, 6 }, { 2, 3 }, { 2, 5 }, { 3, 8 } };
int n = 4;
// function call
cout << maxOccurring(range, n);
return 0;
}
Java
// Java program to check the most occurring
// element in given range
class GFG
{
// Function that returns the maximum element.
static int maxOccurring(int range[][], int n)
{
// freq array to store the frequency
int []freq = new int[(int)(1e6 + 2)];
int first = 0, last = 0;
// iterate and mark the hash array
for (int i = 0; i < n; i++)
{
int l = range[i][0];
int r = range[i][1];
// increase the hash array by 1 at L
freq[l] += 1;
// Decrease the hash array by 1 at R
freq[r + 1] -= 1;
first = Math.min(first, l);
last = Math.max(last, r);
}
// stores the maximum frequency
int maximum = 0;
int element = 0;
// check for the most occurring element
for (int i = first+1; i <= last; i++)
{
// increase the frequency
freq[i] = freq[i - 1] + freq[i];
// check if is more than the previous one
if (freq[i] > maximum)
{
maximum = freq[i];
element = i;
}
}
return element;
}
// Driver code
public static void main(String[] args)
{
int range[][] = { { 1, 6 }, { 2, 3 },
{ 2, 5 }, { 3, 8 } };
int n = 4;
// function call
System.out.println(maxOccurring(range, n));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to check the most
# occurring element in given range
# Function that returns the
# maximum element.
def maxOccurring(range1, n):
# freq array to store the frequency
freq = [0] * 1000002;
first = 0;
last = 0;
# iterate and mark the hash array
for i in range(n):
l = range1[i][0];
r = range1[i][1];
# increase the hash array by 1 at L
freq[l] += 1;
# Decrease the hash array by 1 at R
freq[r + 1] -= 1;
first = min(first, l);
last = max(last, r);
# stores the maximum frequency
maximum = 0;
element = 0;
# check for the most occurring element
for i in range(first, last + 1):
# increase the frequency
freq[i] = freq[i - 1] + freq[i];
# check if is more than the
# previous one
if(freq[i] > maximum):
maximum = freq[i];
element = i;
return element;
# Driver code
range1= [[ 1, 6 ], [ 2, 3 ],
[ 2, 5 ], [ 3, 8 ]];
n = 4;
# function call
print(maxOccurring(range1, n));
# This code is contributed by mits
C#
// C# program to check the most occurring
// element in given range
using System;
class GFG
{
// Function that returns the maximum element.
static int maxOccurring(int [,]range, int n)
{
// freq array to store the frequency
int []freq = new int[(int)(1e6 + 2)];
int first = 0, last = 0;
// iterate and mark the hash array
for (int i = 0; i < n; i++)
{
int l = range[i, 0];
int r = range[i, 1];
// increase the hash array by 1 at L
freq[l] += 1;
// Decrease the hash array by 1 at R
freq[r + 1] -= 1;
first = Math.Min(first, l);
last = Math.Max(last, r);
}
// stores the maximum frequency
int maximum = 0;
int element = 0;
// check for the most occurring element
for (int i = first + 1; i <= last; i++)
{
// increase the frequency
freq[i] = freq[i - 1] + freq[i];
// check if is more than the previous one
if (freq[i] > maximum)
{
maximum = freq[i];
element = i;
}
}
return element;
}
// Driver code
public static void Main(String[] args)
{
int [,]range = {{ 1, 6 }, { 2, 3 },
{ 2, 5 }, { 3, 8 }};
int n = 4;
// function call
Console.WriteLine(maxOccurring(range, n));
}
}
// This code is contributed by Princi Singh
PHP
$maximum)
{
$maximum = $freq[$i];
$element = $i;
}
}
return $element;
}
// Driver code
$range = array(array( 1, 6 ),
array( 2, 3 ),
array( 2, 5 ),
array( 3, 8 ));
$n = 4;
// function call
echo maxOccurring($range, $n);
// This code is contributed by ita_c
?>
Javascript
3
注意:如果 L 和 T 的值为 10 8的值,则上述方法将不起作用,因为会出现内存错误。对于这些限制,我们需要一种不同但相似的方法。您可能会考虑散列。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。