给定L到R形式的N 个范围,任务是找到所有范围中出现的最大整数。如果存在多个这样的整数,则打印最小的一个。
例子:
Input: points[] = { {1, 6}, {2, 3}, {2, 5}, {3, 8} }
Output: 3
Explaination :
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}
Hence, the most occured integer is 3.
Input: points[] = { {1, 4}, {1, 9}, {1, 2}};
Output: 1
方法:这是在 n 范围内出现的最大整数的临时方法。主要思想是使用坐标压缩 散列。因此,无需为大范围创建超出内存限制的频率数组。下面是解决这个问题的分步算法:
- 初始化一个有序的地图名称points ,以跟踪给定范围的起点和终点。
- 对于给定范围的每个起始索引,将point[L]的值增加1 。
- 对于给定范围的每个结束索引,将point[R+1]的值减少1 。
- 按点值的递增顺序迭代地图。请注意,频率[当前点] = 频率[上一个点] + 点[当前点] 。在变量cur_freq 中维护当前点的频率值。
- cur_freq具有最大值的点将是答案。
- 存储索引并返回它。
下面是上述方法的实现:
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 现场工作专业课程和学生竞争性编程现场课程。