[LeetCode] Sort the Jumbled Numbers

2191. Sort the Jumbled Numbers

You are given a 0-indexed integer array mapping which represents the mapping rule of a shuffled decimal system. mapping[i] = j means digit i should be mapped to digit j in this system.

The mapped value of an integer is the new integer obtained by replacing each occurrence of digit i in the integer with mapping[i] for all 0 <= i <= 9.

You are also given another integer array nums. Return the array nums sorted in non-decreasing order based on the mapped values of its elements.

Notes:

  • Elements with the same mapped values should appear in the same relative order as in the input.
  • The elements of nums should only be sorted based on their mapped values and not be replaced by them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
int convert(vector<int>& mapping, int n) {
string num = to_string(n);
for(int i = 0; i < num.size(); i++) {
num[i] = mapping[num[i] - '0'] + '0';
}
return stoi(num);
}
public:
vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {
priority_queue<array<int,3>, vector<array<int,3>>, greater<array<int,3>>> pq;
for(int i = 0; i < nums.size(); i++) {
int cvt = convert(mapping, nums[i]);

pq.push({cvt, i, nums[i]});
}
vector<int> res;
while(!pq.empty()) {
auto [cvt, idx, num] = pq.top(); pq.pop();
res.push_back(num);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/06/PS/LeetCode/sort-the-jumbled-numbers/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.