2343. Query Kth Smallest Trimmed Number Medium

@problem@discussion
#Array#String#Divide and Conquer#Sorting#Heap (Priority Queue)#Radix Sort#Quickselect



1/**
2 * [2343] Query Kth Smallest Trimmed Number
3 *
4 * You are given a 0-indexed array of strings nums, where each string is of equal length and consists of only digits.
5 * You are also given a 0-indexed 2D integer array queries where queries[i] = [ki, trimi]. For each queries[i], you need to:
6 * 
7 * 	Trim each number in nums to its rightmost trimi digits.
8 * 	Determine the index of the ki^th smallest trimmed number in nums. If two trimmed numbers are equal, the number with the lower index is considered to be smaller.
9 * 	Reset each number in nums to its original length.
10 * 
11 * Return an array answer of the same length as queries, where answer[i] is the answer to the i^th query.
12 * Note:
13 * 
14 * 	To trim to the rightmost x digits means to keep removing the leftmost digit, until only x digits remain.
15 * 	Strings in nums may contain leading zeros.
16 * 
17 *  
18 * Example 1:
19 * 
20 * Input: nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]]
21 * Output: [2,2,1,0]
22 * Explanation:
23 * 1. After trimming to the last digit, nums = ["2","3","1","4"]. The smallest number is 1 at index 2.
24 * 2. Trimmed to the last 3 digits, nums is unchanged. The 2^nd smallest number is 251 at index 2.
25 * 3. Trimmed to the last 2 digits, nums = ["02","73","51","14"]. The 4^th smallest number is 73.
26 * 4. Trimmed to the last 2 digits, the smallest number is 2 at index 0.
27 *    Note that the trimmed number "02" is evaluated as 2.
28 * 
29 * Example 2:
30 * 
31 * Input: nums = ["24","37","96","04"], queries = [[2,1],[2,2]]
32 * Output: [3,0]
33 * Explanation:
34 * 1. Trimmed to the last digit, nums = ["4","7","6","4"]. The 2^nd smallest number is 4 at index 3.
35 *    There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3.
36 * 2. Trimmed to the last 2 digits, nums is unchanged. The 2^nd smallest number is 24.
37 * 
38 *  
39 * Constraints:
40 * 
41 * 	1 <= nums.length <= 100
42 * 	1 <= nums[i].length <= 100
43 * 	nums[i] consists of only digits.
44 * 	All nums[i].length are equal.
45 * 	1 <= queries.length <= 100
46 * 	queries[i].length == 2
47 * 	1 <= ki <= nums.length
48 * 	1 <= trimi <= nums[i].length
49 * 
50 *  
51 * Follow up: Could you use the Radix Sort Algorithm to solve this problem? What will be the complexity of that solution?
52 * 
53 */
54pub struct Solution {}
55
56// problem: https://leetcode.com/problems/query-kth-smallest-trimmed-number/
57// discuss: https://leetcode.com/problems/query-kth-smallest-trimmed-number/discuss/?currentPage=1&orderBy=most_votes&query=
58
59// submission codes start here
60
61impl Solution {
62    pub fn smallest_trimmed_numbers(nums: Vec<String>, queries: Vec<Vec<i32>>) -> Vec<i32> {
63        vec![]
64    }
65}
66
67// submission codes end
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_2343() {
75    }
76}
77


Back
© 2025 bowen.ge All Rights Reserved.