由所有最大出现元素的所有出现组成的最小子数组的长度
给定一个大小为N的数组arr[] ,任务是找到由所有出现的最大元素组成的最小子数组的长度
例子:
Input: arr[] = {1, 2, 1, 3, 2}
Output: 5
Explanation: Elements with maximum frequency (=2) are 1 & 2.
Therefore, the length of smallest subarray consisting of all the occurrences of 1 and 2 is 5 i.e {1, 2, 1, 3, 2}
Input: arr[] = {1, 2, 5, 1, 5, 5}
Output: 4
方法:可以通过跟踪最大出现元素的第一次和最后一次出现来解决任务。最小子数组的长度将是最后一次出现的最大值与第一次出现的最小值之间的差。
请按照以下步骤解决问题:
- 创建一个地图,存储元素的频率
- 找到具有最大频率的元素,并存储它们的第一次和最后一次出现。
- 最后,返回最后一次出现的最大值和第一次出现的最小值之间的差
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the length of smallest
// subarray consisting of all the occurrences
// of maximum occurring elements
int get(int arr[], int n)
{
// Stores the frequencies
unordered_map occ;
// Stores the maximum frequency
int mx = -1;
for (int i = 0; i < n; i++) {
occ[arr[i]]++;
mx = max(mx, occ[arr[i]]);
}
// Stores the maximum occurring elements
unordered_map chk;
for (auto x : occ) {
if (x.second == mx)
chk[x.first]++;
}
// Stores the minimum of first occurrences
// and maximum of last occurrences
// of all the maximum occurring elements
int fr = INT_MAX, sc = INT_MIN;
for (int i = 0; i < n; i++) {
// Maximum occurring element
if (chk.find(arr[i]) != chk.end()) {
fr = min(fr, i);
sc = max(sc, i);
}
}
return sc - fr + 1;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 5, 1, 5, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << get(arr, n);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the length of smallest
// subarray consisting of all the occurrences
// of maximum occurring elements
static int get(int arr[], int n)
{
// Stores the frequencies
HashMap occ = new HashMap();
// Stores the maximum frequency
int mx = -1;
for (int i = 0; i < n; i++) {
if(occ.containsKey(arr[i])){
occ.put(arr[i], occ.get(arr[i])+1);
}
else{
occ.put(arr[i], 1);
}
mx = Math.max(mx, occ.get(arr[i]));
}
// Stores the maximum occurring elements
HashMap chk= new HashMap();
for (Map.Entry x : occ.entrySet()) {
if (x.getValue() == mx)
if(chk.containsKey(x.getKey())){
chk.put(x.getKey(), chk.get(x.getKey())+1);
}
else{
chk.put(x.getKey(), 1);
}
}
// Stores the minimum of first occurrences
// and maximum of last occurrences
// of all the maximum occurring elements
int fr = Integer.MAX_VALUE, sc = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
// Maximum occurring element
if (chk.containsKey(arr[i])) {
fr = Math.min(fr, i);
sc = Math.max(sc, i);
}
}
return sc - fr + 1;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 5, 1, 5, 5 };
int n = arr.length;
System.out.print(get(arr, n));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python 3 program for the above approach
import sys
from collections import defaultdict
# Function to find the length of smallest
# subarray consisting of all the occurrences
# of maximum occurring elements
def get(arr, n):
# Stores the frequencies
occ = defaultdict(int)
# Stores the maximum frequency
mx = -1
for i in range(n):
occ[arr[i]] += 1
mx = max(mx, occ[arr[i]])
# Stores the maximum occurring elements
chk = defaultdict(int)
for x in occ:
if (occ[x] == mx):
chk[x] += 1
# Stores the minimum of first occurrences
# and maximum of last occurrences
# of all the maximum occurring elements
fr = sys.maxsize
sc = -sys.maxsize
for i in range(n):
# Maximum occurring element
if (arr[i] in chk):
fr = min(fr, i)
sc = max(sc, i)
return sc - fr + 1
# Driver Code
if __name__ == "__main__":
arr = [1, 2, 5, 1, 5, 5]
n = len(arr)
print(get(arr, n))
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// Function to find the length of smallest
// subarray consisting of all the occurrences
// of maximum occurring elements
static int get(int []arr, int n)
{
// Stores the frequencies
Dictionary occ =
new Dictionary();
// Stores the maximum frequency
int mx = -1;
for (int i = 0; i < n; i++) {
if (occ.ContainsKey(arr[i]))
{
occ[arr[i]] = occ[arr[i]] + 1;
}
else
{
occ.Add(arr[i], 1);
}
mx = Math.Max(mx, occ[arr[i]]);
}
// Stores the maximum occurring elements
Dictionary chk =
new Dictionary();
foreach (KeyValuePair x in occ){
if (x.Value == mx){
if(chk.ContainsKey(x.Key)){
chk[x.Key] = chk[x.Key] + 1;
}
else{
chk.Add(x.Key, 1);
}
}
}
// Stores the minimum of first occurrences
// and maximum of last occurrences
// of all the maximum occurring elements
int fr = Int32.MaxValue, sc = Int32.MinValue;
for (int i = 0; i < n; i++) {
// Maximum occurring element
if (chk.ContainsKey(arr[i])) {
fr = Math.Min(fr, i);
sc = Math.Max(sc, i);
}
}
return sc - fr + 1;
}
// Driver Code
public static void Main()
{
int []arr = { 1, 2, 5, 1, 5, 5 };
int n = arr.Length;
Console.Write(get(arr, n));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
4
时间复杂度:O(N)
辅助空间: O(N)