通过给定的替换使给定的 Array 排列为 1 到 N 的成本最小化
给定两个长度为N的数组a[]和b[]以及一个整数K (1 ≤ K ≤ N) 。数组a[]中的所有整数都在[1, K]范围内。数组b[]中的第 i 个值表示将a[i]替换为[1, N]范围内的任何数字的成本。任务是找到替换数组a[]的元素以将其转换为1 到 N的排列的最小成本。
注意: 1 到 N的排列包含从1 到 N的所有值以任意顺序,并且没有重复值。
例子:
Input: K = 7, a[] = {1, 1, 3, 4, 5, 3, 7, 1}
b[] = {7, 5, 4, 8, 1, 3, 5, 2}
Output: 10
Explanation: In a[] some numbers are repeating which are 1, 1, 3, 3, 1.
Now, make two 1’s and one 3 unique.
Select a[1], a[5] and a[7] and replace them with 2, 6, and 8 to make array a permutation of 1 to 8.
The total cost is b[1] + b[5] + b[7] = 5 + 3 + 2 = 10.
This is the minimum cost to make a[] a permutation of 1 to 8.
Now, a[] becomes {1, 2, 3, 4, 5, 6, 7, 8}
Input: K = 3, a[] = {3, 1, 2}
b[] = {5, 3, 4}
Output: 0
Explanation: a[] is already a permutation of 1 to 3. So no need to replace any value.
方法:问题的解决方法是基于散列的概念。存储重复的元素并替换除具有最大替换成本的元素之外的所有元素。请按照以下步骤解决问题:
- 初始化地图以存储a[i]和b[i] 。
- 初始化向量以存储最小答案。
- 现在遍历两个数组。
- 如果映射中不存在a[i] ,则将a[i] 作为键存储,将b[i]作为值存储在映射中。
- 否则,如果存在a[i]并且存储在映射中的 a[i] 的值小于b[i] ,则将 a [ i] 的现有值存储在向量v中并将映射中的值更改为b[我] 。
- 否则将b[i]存储在向量v中。
- 对向量v进行排序。
- 初始化变量ans = 0。
- 现在遍历向量(K - 地图的大小)次并将向量的所有值求和ans 。
下面是上述方法的实现。
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to calculate the minimum cost
int minCost(int a[], int b[], int N, int K)
{
// Initialize map and vector
map m;
vector v;
for (int i = 0; i < N; i++) {
if (m[a[i]] == 0) {
m[a[i]] = b[i];
}
else {
if (m[a[i]] < b[i]) {
v.push_back(m[a[i]]);
m[a[i]] = b[i];
}
else {
v.push_back(b[i]);
}
}
}
sort(v.begin(), v.end());
int size = K - m.size();
int ans = 0;
for (int i = 0; i < size; i++) {
ans += v[i];
}
return ans;
}
// Driver code
int main()
{
int a[] = { 1, 1, 3, 1, 5, 3, 7, 1 };
int b[] = { 5, 7, 4, 8, 1, 3, 5, 2 };
int K = 7;
int N = sizeof(a) / sizeof(a[0]);
cout << minCost(a, b, N, K);
return 0;
}
Java
// JAVA code to implement the approach
import java.util.*;
class GFG
{
// Function to calculate the minimum cost
public static int minCost(int[] a, int[] b, int N,
int K)
{
// Initialize map and ArrayList
HashMap m
= new HashMap();
ArrayList v = new ArrayList();
for (int i = 0; i < N; i++) {
if (m.containsKey(a[i])) {
m.put(a[i], 0);
}
}
for (int i = 0; i < N; i++) {
if (m.get(a[i]) == null) {
m.put(a[i], b[i]);
}
else {
if (m.get(a[i]) < b[i]) {
v.add(m.get(a[i]));
m.put(a[i], b[i]);
}
else {
v.add(b[i]);
}
}
}
Collections.sort(v);
int size = K - m.size();
int ans = 0;
for (int i = 0; i < size; i++) {
ans += v.get(i);
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int[] a = new int[] { 1, 1, 3, 1, 5, 3, 7, 1 };
int[] b = new int[] { 5, 7, 4, 8, 1, 3, 5, 2 };
int K = 7;
int N = a.length;
System.out.print(minCost(a, b, N, K));
}
}
// This code is contributed by Taranpreet
C#
// C# code to implement the approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// Function to calculate the minimum cost
static int minCost(int []a, int []b, int N, int K)
{
// Initialize map and vector
Dictionary m =
new Dictionary();
ArrayList v = new ArrayList();
for(int i = 0; i < N; i++) {
if(!m.ContainsKey(a[i])) {
m.Add(a[i], 0);
}
}
for (int i = 0; i < N; i++) {
if (m[a[i]] == 0) {
m[a[i]] = b[i];
}
else {
if (m[a[i]] < b[i]) {
v.Add(m[a[i]]);
m[a[i]] = b[i];
}
else {
v.Add(b[i]);
}
}
}
v.Sort();
int size = K - m.Count;
int ans = 0;
for (int i = 0; i < size; i++) {
ans += (int)v[i];
}
return ans;
}
// Driver code
public static void Main()
{
int []a = { 1, 1, 3, 1, 5, 3, 7, 1 };
int []b = { 5, 7, 4, 8, 1, 3, 5, 2 };
int K = 7;
int N = a.Length;
Console.Write(minCost(a, b, N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
Python3
# Python code for the above approach
# Function to calculate the minimum cost
def minCost(a, b, N, K):
# Initialize map and vector
m = {}
v = [];
for i in range(N):
if (a[i] not in m):
m[a[i]] = b[i]
else:
if (m[a[i]] < b[i]):
v.append(m[a[i]]);
m[a[i]] = b[i]
else:
v.append(b[i]);
v.sort()
size = K - len(m);
ans = 0;
for i in range(size):
ans += v[i];
return ans;
# Driver code
a = [1, 1, 3, 1, 5, 3, 7, 1];
b = [5, 7, 4, 8, 1, 3, 5, 2];
K = 7;
N = len(a)
print(minCost(a, b, N, K));
# This code is contributed by Saurabh Jaiswal
10
时间复杂度: O(N * log N)
辅助空间: O(N)