#2343
Medium Algorithms Query kth smallest trimmed number
Array String Divide and Conquer Sorting Heap (Priority Queue) Radix Sort Quickselect
47.1% acceptance
Feb 25, 2026
345
443
You are given a 0-indexed array of strings nums, where each string is of equal length and consists of only digits.
You are also given a 0-indexed 2D integer array queries where queries[i] = [ki, trimi]. For each queries[i], you need to:
Trim each number in nums to its rightmost trimi digits.
Determine the index of the kith smallest trimmed number in nums. If two trimmed numbers are equal, the number with the lower index is considered to be smaller.
Reset each number in nums to its original length.
Return an array answer of the same length as queries, where answer[i] is the answer to the ith query.
Note:
To trim to the rightmost x digits means to keep removing the leftmost digit, until only x digits remain.
Strings in nums may contain leading zeros.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn smallest_trimmed_numbers(nums: Vec<String>, queries: Vec<Vec<i32>>) -> Vec<i32> {
let len = nums[0].len();
queries.iter().map(|q| {
let (k, trim) = (q[0] as usize, q[1] as usize);
let start = len - trim;
let mut indexed: Vec<(&str, usize)> = nums.iter().enumerate()
.map(|(i, s)| (&s[start..], i))
.collect();
indexed.sort_unstable();
indexed[k - 1].1 as i32
}).collect()
}
}