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 rightmosttrimi 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 arrayanswerof the same length asqueries,whereanswer[i]is the answer to theithquery.
Note :
To trim to the rightmost x digits means to keep removing the leftmost digit, until only x digits remain.
Input: nums =["102","473","251","814"], queries =[[1,1],[2,3],[4,2],[1,2]]Output: [2,2,1,0]Explanation:
1. After trimming to the last digit, nums =["2","3","1","4"]. The smallest number is1 at index 2.2. Trimmed to the last 3 digits, nums is unchanged. The 2nd smallest number is251 at index 2.3. Trimmed to the last 2 digits, nums =["02","73","51","14"]. The 4th smallest number is73.4. Trimmed to the last 2 digits, the smallest number is2 at index 0. Note that the trimmed number "02"is evaluated as 2.
Input: nums =["24","37","96","04"], queries =[[2,1],[2,2]]Output: [3,0]Explanation:
1. Trimmed to the last digit, nums =["4","7","6","4"]. The 2nd smallest number is4 at index 3. There are two occurrences of 4, but the one at index 0is considered smaller than the one at index 3.2. Trimmed to the last 2 digits, nums is unchanged. The 2nd smallest number is24.
For each query, we can trim all numbers, pair them with their original indices, sort, and pick the k-th smallest. Since the constraints are small, this is efficient enough.
classSolution {
publicint[]smallestTrimmedNumbers(String[] nums, int[][] queries) {
int[] ans =newint[queries.length];
for (int q = 0; q < queries.length; ++q) {
int k = queries[q][0], trim = queries[q][1];
int n = nums.length;
String[][] arr =new String[n][2];
for (int i = 0; i < n; ++i) {
arr[i][0]= nums[i].substring(nums[i].length() - trim);
arr[i][1]= String.valueOf(i);
}
Arrays.sort(arr, (a, b) -> a[0].equals(b[0]) ? Integer.parseInt(a[1]) - Integer.parseInt(b[1]) : a[0].compareTo(b[0]));
ans[q]= Integer.parseInt(arr[k-1][1]);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
classSolution {
funsmallestTrimmedNumbers(nums: Array<String>, queries: Array<IntArray>): IntArray {
return queries.map { (k, trim) -> nums.mapIndexed { i, s -> s.takeLast(trim) to i }
.sortedWith(compareBy({ it.first }, { it.second }))
[k-1].second
}.toIntArray()
}
}
1
2
3
4
5
6
7
8
9
from typing import List
classSolution:
defsmallestTrimmedNumbers(self, nums: List[str], queries: List[List[int]]) -> List[int]:
ans = []
for k, trim in queries:
arr = [(num[-trim:], i) for i, num in enumerate(nums)]
arr.sort()
ans.append(arr[k-1][1])
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnsmallest_trimmed_numbers(nums: Vec<String>, queries: Vec<Vec<i32>>) -> Vec<i32> {
letmut ans =vec![];
for q in queries {
let k = q[0] asusize;
let trim = q[1] asusize;
letmut arr: Vec<(String, usize)>= nums.iter().enumerate().map(|(i, s)| (s[s.len()-trim..].to_string(), i)).collect();
arr.sort();
ans.push(arr[k-1].1asi32);
}
ans
}
}
⏰ Time complexity: O(Q * N log N * L), where Q is the number of queries, N is the number of numbers, and L is the max length of a number. Each query sorts N strings of length up to L.
🧺 Space complexity: O(N), for storing the trimmed numbers per query.