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

📅  最后修改于: 2021-10-27 08:58:51             🧑  作者: Mango

给定LR形式的N 个范围,任务是找到所有范围中出现的最大整数。如果存在多个这样的整数,则打印最小的一个。

例子:

方法:这是在 n 范围内出现的最大整数的临时方法。主要思想是使用坐标压缩 散列。因此,无需为大范围创建超出内存限制的频率数组。下面是解决这个问题的分步算法:

  1. 初始化一个有序的地图名称points ,以跟踪给定范围的起点和终点。  
  2. 对于给定范围的每个起始索引,将point[L]的值增加1
  3. 对于给定范围的每个结束索引,将point[R+1]的值减少1
  4. 按点值的递增顺序迭代地图。请注意,频率[当前点] = 频率[上一个点] + 点[当前点] 。在变量cur_freq 中维护当前点的频率值。
  5. cur_freq具有最大值的点将是答案。
  6. 存储索引并返回它。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function to print the most
// occured element in given ranges
void maxOccurring(int range[][2], int n)
{
    // STL map for storing start
    // and end points
    map points;
 
    for (int i = 0; i < n; i++) {
        int l = range[i][0];
        int r = range[i][1];
 
        // Increment at starting point
        points[l]++;
 
        // Decrement at ending point
        points[r + 1]--;
    }
 
    // Stores current frequncy
    int cur_freq = 0;
 
    // Store element with maximum frequncy
    int ans = 0;
 
    // Frequency of the current ans
    int freq_ans = 0;
 
    for (auto x : points) {
        // x.first denotes the current
        // point and x.second denotes
        // points[x.first]
        cur_freq += x.second;
 
        // If frequency of x.first is
        // greater that freq_ans
        if (cur_freq > freq_ans) {
            freq_ans = cur_freq;
            ans = x.first;
        }
    }
 
    // Print Answer
    cout << ans;
}
 
// Driver code
int main()
{
    int range[][2]
        = { { 1, 6 }, { 2, 3 }, { 2, 5 }, { 3, 8 } };
    int n = sizeof(range) / sizeof(range[0]);
 
    // Function call
    maxOccurring(range, n);
 
    return 0;
}


Java
// Java program of the above approach
import java.util.*;
 
class GFG{
 
// Function to print the most
// occured element in given ranges
static void maxOccurring(int range[][], int n)
{
    // STL map for storing start
    // and end points
    HashMap points = new HashMap();
 
    for (int i = 0; i < n; i++) {
        int l = range[i][0];
        int r = range[i][1];
 
        // Increment at starting point
        if(points.containsKey(l)){
            points.put(l, points.get(l)+1);
        }
        else{
            points.put(l, 1);
 
        // Decrement at ending point
            if(points.containsKey(r + 1)){
                points.put(r + 1, points.get(r + 1)-1);
            }
    }
    }
    // Stores current frequncy
    int cur_freq = 0;
 
    // Store element with maximum frequncy
    int ans = 0;
 
    // Frequency of the current ans
    int freq_ans = 0;
 
    for (Map.Entry entry :points.entrySet()) {
        // x.first denotes the current
        // point and x.second denotes
        // points[x.first]
        cur_freq += entry.getValue();
 
        // If frequency of x.first is
        // greater that freq_ans
        if (cur_freq > freq_ans) {
            freq_ans = cur_freq;
            ans = entry.getKey();
        }
    }
 
    // Print Answer
    System.out.print(ans);
}
 
// Driver code
public static void main(String[] args)
{
    int range[][]
        = { { 1, 6 }, { 2, 3 }, { 2, 5 }, { 3, 8 } };
    int n = range.length;
 
    // Function call
    maxOccurring(range, n);
 
}
}
 
// This code is contributed by Princi Singh


Python3
# Python 3 program of the above approach
 
# Function to print the most
# occured element in given ranges
def maxOccurring(range1, n):
   
    # STL map for storing start
    # and end points
    points = {}
 
    for i in range(n):
        l = range1[i][0]
        r = range1[i][1]
 
        # Increment at starting point
        if l in points:
            points[l] += 1
        else:
            points[l] = 1
 
        # Decrement at ending point
        if r+1 in points:
            points[r + 1] -= 1
        else:
            points[r+1] = 0
 
    # Stores current frequncy
    cur_freq = 0
 
    # Store element with maximum frequncy
    ans = 0
 
    # Frequency of the current ans
    freq_ans = 0
 
    for key,value in points.items():
        # x.first denotes the current
        # point and x.second denotes
        # points[x.first]
        cur_freq += value
 
        # If frequency of x.first is
        # greater that freq_ans
        if (cur_freq > freq_ans):
            freq_ans = cur_freq
            ans = key
 
    # Print Answer
    print(ans)
 
# Driver code
if __name__ == '__main__':
    range1 = [[1, 6],[2, 3],[2, 5],[3, 8]]
    n = len(range1)
 
    # Function call
    maxOccurring(range1, n)
     
    # This code is contributed by ipg2016107.


输出
3

时间复杂度: O(n Logn)
空间复杂度: O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程