// return dth character of s, 0 if d >= length of string privatestaticintcharAt(String s, int idx) { if (idx < 0 || idx >= s.length()) return0; return s.charAt(idx); }
privatestaticvoidMSDsort(String[] arr, int l, int r, int d, String[] aux) { if (l >= r) return; int[] count = newint[256]; // compute frequency of dth character in each string for (inti= l; i <= r; i++) { count[charAt(arr[i], d)+1]++; } // compute cumulates for (inti=0; i < 255; i++) { count[i+1] += count[i]; } // distribute for (inti= l; i <= r; i++) { aux[count[charAt(arr[i], d)]++] = arr[i]; } for (inti= l; i <= r; i++) { arr[i] = aux[i]; } // recursively sort for each character for (inti=0; i < 255; i++) { MSDsort(arr, l+count[i], l+count[i+1]-1, d+1, aux); } }
2343. Query Kth Smallest Trimmed Number
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] = [k_i, trim_i]$. For each queries[i], you need to:
Trim each number in nums to its rightmost trimi digits.
Determine the index of the $k_i^{th}$ 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 $i^{th}$ query.
Example 1:
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 is 1 at index 2. 2. Trimmed to the last 3 digits, nums is unchanged. The 2nd smallest number is 251 at index 2. 3. Trimmed to the last 2 digits, nums = ["02","73","51","14"]. The 4th smallest number is 73. 4. Trimmed to the last 2 digits, the smallest number is 2 at index 0.
publicint[] smallestTrimmedNumbers(String[] nums, int[][] queries) { Map<Integer, List<int[]>> map = newHashMap<>(); // trim -> ith smalllest, index; intn= nums.length, m = queries.length, k = nums[0].length(); for (inti=0; i < k; i++) { map.put(i, newArrayList<>()); } for (inti=0; i < m; i++) { map.get(k-queries[i][1]).add(newint[]{queries[i][0]-1, i}); } int[] res = newint[m], index = newint[n], aux = newint[n]; for (inti=1; i < n; i++) { index[i] = i; } for (intd= k-1; d >= 0; d--) { int[] count = newint[R+1]; for (inti=0; i < n; i++) { charc= nums[index[i]].charAt(d); count[c+1]++; } for (inti=0; i < R; i++) { count[i+1] += count[i]; } for (inti=0; i < n; i++) { charc= nums[index[i]].charAt(d); aux[count[c]++] = index[i]; } int[] t = index; index = aux; aux = t; List<int[]> q = map.get(d); for (int[] p: q) { res[p[1]] = index[p[0]]; } } return res; } }
164. Maximum Gap
Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array contains less than two elements, return 0.
You must write an algorithm that runs in linear time and uses linear extra space.
Example 1:
Input: nums = [3,6,9,1] Output: 3 Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.