📌  相关文章
📜  n 个范围内出现的最大整数 |组 2

📅  最后修改于: 2021-10-27 09:04:44             🧑  作者: Mango

给定 N 个 LR 范围。任务是打印在给定范围内出现次数最多的数字。
注: 1 <= L <= R <= 10 6
例子:

方法:该方法类似于 n 范围内出现的最大整数。唯一不同的是找到范围的下限和上限。这样就不需要从 1 遍历到 MAX。
下面是解决这个问题的分步算法:

  1. 用 0 初始化一个 freq 数组,让数组的大小为 10^6,因为这是可能的最大值。
  2. 对于给定范围的每个起始索引,将freq[l] 增加 1
  3. 对于给定范围的每个结束索引,将freq[r+1] 减少 1
  4. 从最小 L 迭代到最大 R 并通过freq[i] += freq[i-1]添加频率。
  5. 具有最大值 freq[i] 的索引将是答案。
  6. 存储索引并返回它。

下面是上述方法的实现:

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 现场工作专业课程学生竞争性编程现场课程