Skip to content

Commit 46a0288

Browse files
committed
2191. Sort the Jumbled Numbers: RETRY
1 parent 572cae5 commit 46a0288

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,3 +1656,4 @@ mod s2186_minimum_number_of_steps_to_make_two_strings_anagram_ii;
16561656
mod s2187_minimum_time_to_complete_trips;
16571657
mod s2188_minimum_time_to_finish_the_race;
16581658
mod s2190_most_frequent_number_following_key_in_an_array;
1659+
mod s2191_sort_the_jumbled_numbers;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* [2191] Sort the Jumbled Numbers
3+
*
4+
* 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.
5+
* 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.
6+
* 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.
7+
* Notes:
8+
*
9+
* Elements with the same mapped values should appear in the same relative order as in the input.
10+
* The elements of nums should only be sorted based on their mapped values and not be replaced by them.
11+
*
12+
*
13+
* Example 1:
14+
*
15+
* Input: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
16+
* Output: [338,38,991]
17+
* Explanation:
18+
* Map the number 991 as follows:
19+
* 1. mapping[9] = 6, so all occurrences of the digit 9 will become 6.
20+
* 2. mapping[1] = 9, so all occurrences of the digit 1 will become 9.
21+
* Therefore, the mapped value of 991 is 669.
22+
* 338 maps to 007, or 7 after removing the leading zeros.
23+
* 38 maps to 07, which is also 7 after removing leading zeros.
24+
* Since 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38.
25+
* Thus, the sorted array is [338,38,991].
26+
*
27+
* Example 2:
28+
*
29+
* Input: mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]
30+
* Output: [123,456,789]
31+
* Explanation: 789 maps to 789, 456 maps to 456, and 123 maps to 123. Thus, the sorted array is [123,456,789].
32+
*
33+
*
34+
* Constraints:
35+
*
36+
* mapping.length == 10
37+
* 0 <= mapping[i] <= 9
38+
* All the values of mapping[i] are unique.
39+
* 1 <= nums.length <= 3 * 10^4
40+
* 0 <= nums[i] < 10^9
41+
*
42+
*/
43+
pub struct Solution {}
44+
45+
// problem: https://leetcode.com/problems/sort-the-jumbled-numbers/
46+
// discuss: https://leetcode.com/problems/sort-the-jumbled-numbers/discuss/?currentPage=1&orderBy=most_votes&query=
47+
48+
// submission codes start here
49+
50+
impl Solution {
51+
pub fn sort_jumbled(mapping: Vec<i32>, nums: Vec<i32>) -> Vec<i32> {
52+
vec![]
53+
}
54+
}
55+
56+
// submission codes end
57+
58+
#[cfg(test)]
59+
mod tests {
60+
use super::*;
61+
62+
#[test]
63+
#[ignore]
64+
fn test_2191_example_1() {
65+
let mapping = vec![8, 9, 4, 0, 2, 1, 3, 5, 7, 6];
66+
let nums = vec![991, 338, 38];
67+
68+
let result = vec![338, 38, 991];
69+
70+
assert_eq!(Solution::sort_jumbled(mapping, nums), result);
71+
}
72+
73+
#[test]
74+
#[ignore]
75+
fn test_2191_example_2() {
76+
let mapping = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
77+
let nums = vec![789, 456, 123];
78+
79+
let result = vec![123, 456, 789];
80+
81+
assert_eq!(Solution::sort_jumbled(mapping, nums), result);
82+
}
83+
}

0 commit comments

Comments
 (0)