给定一个元素数组,并以数组中所有元素都不同的方式更改数组。如果要替换一个值,则替换值应大于前一个值,并且修改后元素之和应尽可能小。
例子:
Input : arr = {1, 2, 2, 5, 8, 8, 8}
Output : 1 2 3 5 8 9 10
8 is replaced with 9 (A non-existing element greater than 8). Next, duplicate occurrence of 8 is replaced with 10.
Input : arr = {1, 2, 5, 7, 8, 8, 7}
Output : 1 2 5 7 8 9 10
Input : arr = {9, 9, 9, 9, 9}
Output : 9 10 11 12 13
资料来源:Paytm 面试经验 30。
此问题是本文的变体。在上面的文章中,我们不得不替换迄今为止价值最大的重复项。但是在这个问题中,我们必须用大于前一个重复值的重复值替换元素。
这个想法是找到下一个更大的元素被替换以最小化总和。为了找到下一个更大的元素,我们必须从重复的元素迭代到INT_MAX,直到找到下一个更大的元素。
下面是上述方法的实现。
C++
// CPP program to replace every repeating
// element with next greater element.
#include
using namespace std;
void replaceElements(int arr[], int n)
{
unordered_set s;
for (int i = 0; i < n; i++) {
// check whether the element is
// repeated or not
if (s.find(arr[i]) == s.end())
s.insert(arr[i]);
else {
// find the next greatest element
for (int j = arr[i] + 1; j < INT_MAX; j++) {
if (s.find(j) == s.end()) {
arr[i] = j;
s.insert(j);
break;
}
}
}
}
}
int main()
{
int arr[] = { 1, 2, 5, 7, 8, 8, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
replaceElements(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << "\n";
}
Java
// Java program to replace every repeating
// element with next greater element.
import java.util.HashSet;
import java.util.Set;
public class ReplaceDuplicateWithGreaterThanPreviousDuplicate {
private static void replaceElements(int[] arr, int n)
{
Set st = new HashSet<>();
for (int i = 0; i < n; i++) {
// check whether the element is
// repeated or not
if (!st.contains(arr[i])) {
st.add(arr[i]);
}
else {
// find the next greatest element
for (int j = arr[i] + 1; j < Integer.MAX_VALUE; j++) {
if (!st.contains(j)) {
arr[i] = j;
st.add(j);
break;
}
}
}
}
}
public static void main(String[] args)
{
int[] arr = new int[] { 1, 2, 5, 7, 8, 8, 7 };
int n = arr.length;
replaceElements(arr, n);
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
Python3
# Python3 program to replace every repeating
# element with next greater element.
import sys
def replaceElements(arr, n):
s = []
for i in range (n):
# check whether the element
# is repeated or not
if arr[i] not in s:
s.append(arr[i])
else :
# find the next greatest element
for j in range(arr[i] + 1, sys.maxsize) :
if j not in s:
arr[i] = j
s.append(j)
break
# Driver Code
if __name__ == "__main__":
arr = [ 1, 2, 5, 7, 8, 8, 7 ]
n = len(arr)
replaceElements(arr, n)
for i in range(n):
print (arr[i], end = " ")
print ()
# This code is contributed
# by ChitraNayal
C#
// C# program to replace every repeating
// element with next greater element.
using System;
using System.Collections.Generic;
public class ReplaceDuplicateWithGreaterThanPreviousDuplicate
{
private static void replaceElements(int[] arr, int n)
{
HashSet st = new HashSet();
for (int i = 0; i < n; i++)
{
// check whether the element is
// repeated or not
if (!st.Contains(arr[i]))
{
st.Add(arr[i]);
}
else
{
// find the next greatest element
for (int j = arr[i] + 1; j < int.MaxValue; j++)
{
if (!st.Contains(j))
{
arr[i] = j;
st.Add(j);
break;
}
}
}
}
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = new int[] { 1, 2, 5, 7, 8, 8, 7 };
int n = arr.Length;
replaceElements(arr, n);
for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
1 2 5 7 8 9 10
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。