用给定的替代数字替换数字后查找数组的排序顺序
给定一个包含N个整数的数组A[]和另一个包含从 0 到 9 的所有数字的替代值的数组M[] (即; M[i] = j表示, j是i的替代值 ( 0 ≤ i, j ≤ 9))、对数组进行如下操作:
- 将数组A[]的所有元素的所有数字替换为其在M[]中给出的替代值。
- 根据元素的新值对数组进行排序。
- 按新值的排序顺序返回以前的值(即用它们以前的值替换新值)。
任务是找到元素的最终排序顺序。
注意:具有相同新值的元素应根据它们在原始数组A[]中的位置进行排序。
例子:
Input: N = 3, A = {991, 338, 38}, M = {8, 9, 4, 0, 2, 1, 3, 5, 7, 6}
Output: {338, 38, 991}
Explanation: Calculation of New Value of 991: M[9] = 6, M[9] = 6, M[1] = 9. So 991 => 669
New value of 338 is 007 i.e. equal to 7.
New value of 38 is 07 or 7.
So, the sorted array of new values is {7, 7, 669}.
So the sorted order is {338, 38, 991}.
Because both 338 and 38 have same value 7 and 338 comes before 38 in actual array.
Input: N = 5, A[] = {20, 25, 10, 5, 77}, M = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Output: {5, 10, 20, 25, 77}
方法:解决思路如下:
Firstly replace the digits with their alternates and then sort the newly formed value in increasing order. Then arrange the original array according to the sorted order of newly formed elements.
请按照以下步骤解决问题:
- 制作一个成对的向量(比如v )。
- 将数组从i = 0 迭代到 N-1 :
- 用M[]中的替代元素替换元素的所有数字并找到新值(比如temp )。
- 制作一对以temp作为第一个元素, A[i]作为第二个元素。
- 将对推入v 。
- 按对的第一个值的升序对向量 v 进行排序(即用替换数字替换数字后新形成的值)。
- 现在排列数组,使 A[i] 与 v[i] 的第二个值相同。
以下是上述方法的实现:
C++
// C++ Code for above approach
#include
using namespace std;
// Function to find new value of an
// integer by mapping digits from M
int newValue(int num, int M[])
{
string s = to_string(num);
int New_Value = 0, x = 0;
for (int i = s.size() - 1; i >= 0; i--) {
New_Value += (M[s[i] - 0]
* pow(10, x));
x++;
}
return New_Value;
}
// Comparator function for sorting
bool comp(pair& p1,
pair& p2)
{
return p1.first < p2.first;
}
// Function to Sort the given array
// according to the New Value of digits
void sortArray(int N, int A[], int M[])
{
// Vector to store pair of elements
// and their new values
vector > v;
// Pushing pairs of New value of elements
// and elements into vector v
for (int i = 0; i < N; i++) {
int New_Value = newValue(A[i], M);
v.push_back({ New_Value, A[i] });
}
// Sorting the vector "v" in
// increasing order of first
// element of pair
sort(v.begin(), v.end(), comp);
// Storing the values of second value
// of vector "v" in array "A"
for (int i = 0; i < N; i++) {
A[i] = v[i].second;
}
for (int i = 0; i < N; i++) {
cout << A[i] << " ";
}
}
// Driver Code
int main()
{
int N = 3;
int A[] = { 991, 338, 38 };
int M[] = { 8, 9, 4, 0, 2, 1, 3, 5, 7, 6 };
// Function call
sortArray(N, A, M);
return 0;
}
Python3
# Python code for the above approach
# Function to find new value of an
# integer by mapping digits from M
from functools import cmp_to_key
import math
def mycmp(a,b):
return a[0] - b[0]
def newValue(num, M):
s = str(num)
New_Value,x = 0,0
for i in range(len(s) - 1,-1,-1):
New_Value += (M[ord(s[i]) - ord('0')]* math.pow(10, x))
x += 1
return New_Value
# Function to Sort the given array
# according to the New Value of digits
def sortArray(N, A, M):
# Vector to store pair of elements
# and their new values
v = []
# Pushing pairs of New value of elements
# and elements into vector v
for i in range(N):
New_Value = newValue(A[i], M)
v.append([New_Value,A[i]])
# Sorting the vector "v" in
# increasing order of first
# element of pair
v.sort(key = cmp_to_key(mycmp))
# Storing the values of second value
# of vector "v" in array "A"
for i in range(N):
A[i] = v[i][1]
for i in range(N):
print(A[i],end = " ")
# Driver Code
N = 3
A = [991, 338, 38]
M = [8, 9, 4, 0, 2, 1, 3, 5, 7, 6]
# Function call
sortArray(N, A, M)
# This code is contributed by shinjanpatra
Javascript
338 38 991
时间复杂度: O(N*log(N))
辅助空间: O(N)