通过在给定数组中以相同距离重新排列所有重复项来最大化距离
给定一个N的数组arr[] 整数。以一种方式排列数组,使得所有相同元素对之间的最小距离在所有可能的排列中最大。任务是找到这个最大值。
例子:
Input: arr[] = {1, 1, 2, 3}
Output: 3
Explanation: All possible arrangements are:
{1, 1, 2, 3}, {1, 1, 3, 2}, {1, 2, 1, 3}, {1, 2, 3, 1}, {1, 3, 1, 2}, {1, 3, 2, 1},
{2, 1, 1, 3}, {2, 1, 3, 1}, {2, 3, 1, 1}, {3, 1, 1, 2}, {3, 1, 2, 1}, {3, 2, 1, 1}.
Here the arrangements {1, 2, 3, 1} and {1, 3, 2, 1} gives the answer which is 3.
As distance = |(index of first 1) – (index of second 1)|
Input: arr[] = {1, 2, 1, 2, 9, 10, 9}
Output: 4
Explanation: One such arrangement is {2, 9, 1, 10, 2, 9, 1}
方法:解决这个问题的方法是基于这样的观察,即具有最大频率的元素之间必须有尽可能远的距离。请按照以下步骤解决问题:
- 找出具有最大频率的元素。
- 假设m个元素具有最大频率max_freq然后从N个位置中征服m*max_freq 个位置。现在未占用的位置是un_pos = N – max_freq*m。
- 现在,将不具有最大频率的元素以相同的顺序连续分组,并且具有相等的间隔。
- 最大间隔可以由interval = un_pos / ( max_freq -1)给出,因为当所有出现的m个元素彼此等距放置时,它将破坏(max_freq – 1)段中的排列。
- 最后,为最高频率元素添加m ,每个元素我们得到给定问题的答案,即间隔+ m。
下面是上述方法的实现。
C++
// C++ code to implement the above approach
#include
using namespace std;
// Function to find
// the maximum possible minimum distance
int find_max_distance(vector a, int n)
{
int m = 0, max_freq = 0;
int un_pos, interval, answer;
// To count frequency of
// each integer value of array
map count_freq;
for (int i = 0; i < n; i++) {
count_freq[a[i]]++;
max_freq = max(max_freq,
count_freq[a[i]]);
}
// Loop to find the number of integers
// in array with maximum frequency
for (auto iterator : count_freq) {
if (iterator.second == max_freq)
m++;
}
un_pos = n - m * max_freq;
interval = un_pos / (max_freq - 1);
answer = interval + m;
return answer;
}
// Driver Code
int main()
{
int N = 7;
vector arr = { 1, 2, 1, 2, 9, 10, 9 };
int result = find_max_distance(arr, N);
cout << result;
return 0;
}
Java
// JAVA code to implement the above approach
import java.util.*;
class GFG
{
// Function to find
// the maximum possible minimum distance
public static int find_max_distance(int[] a, int n)
{
int m = 0, max_freq = 0;
int un_pos, interval, answer;
// To count frequency of
// each integer value of array
HashMap count_freq
= new HashMap<>();
for (int i = 0; i < n; i++) {
if (count_freq.containsKey(a[i])) {
count_freq.put(a[i],
count_freq.get(a[i]) + 1);
}
else {
count_freq.put(a[i], 1);
}
max_freq
= Math.max(max_freq, count_freq.get(a[i]));
}
// Loop to find the number of integers
// in array with maximum frequency
for (Map.Entry iterator :
count_freq.entrySet()) {
if (iterator.getValue() == max_freq)
m++;
}
un_pos = n - m * max_freq;
interval = un_pos / (max_freq - 1);
answer = interval + m;
return answer;
}
// Driver Code
public static void main(String[] args)
{
int N = 7;
int[] arr = new int[] { 1, 2, 1, 2, 9, 10, 9 };
int result = find_max_distance(arr, N);
System.out.print(result);
}
}
// This code is contributed by Taranpreet
Python3
# Python code to implement the above approach
# Function to find
# the maximum possible minimum distance
def find_max_distance(a, n):
m = 0
max_freq = 0
un_pos = 0
interval = 0
answer = 0
# To count frequency of
# each integer value of array
count_freq = {}
for i in range(n):
if a[i] not in count_freq:
count_freq[a[i]] = 1
else:
count_freq[a[i]] += 1
max_freq = max(max_freq,count_freq[a[i]])
# Loop to find the number of integers
# in array with maximum frequency
for i in count_freq:
if (count_freq[i] == max_freq):
m += 1
un_pos = n - m * max_freq
interval = un_pos // (max_freq - 1)
answer = interval + m
return answer
# Driver Code
N = 7
arr = [1, 2, 1, 2, 9, 10, 9]
result = find_max_distance(arr, N)
print(result)
# This code is contributed by rohitsingh07052.
C#
// C# code to implement the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find
// the maximum possible minimum distance
public static int find_max_distance(int[] a, int n)
{
int m = 0, max_freq = 0;
int un_pos = 0, interval = 0, answer = 0;
// To count frequency of
// each integer value of array
Dictionary count_freq
= new Dictionary();
for (int i = 0; i < n; i++) {
if (count_freq.ContainsKey(a[i])) {
count_freq[a[i]] = count_freq[a[i]] + 1;
}
else {
count_freq.Add(a[i], 1);
}
max_freq = Math.Max(max_freq, count_freq[a[i]]);
}
// Loop to find the number of integers
// in array with maximum frequency
foreach(
KeyValuePair iterator in count_freq)
{
if (iterator.Value == max_freq)
m++;
}
un_pos = n - m * max_freq;
interval = un_pos / (max_freq - 1);
answer = interval + m;
return answer;
}
// Driver Code
public static void Main()
{
int N = 7;
int[] arr = new int[] { 1, 2, 1, 2, 9, 10, 9 };
int result = find_max_distance(arr, N);
Console.Write(result);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
4
时间复杂度: O(N)
辅助空间: O(N)