LeetCode-in-Java

2343. Query Kth Smallest Trimmed Number

Medium

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:

Return an array answer of the same length as queries, where answer[i] is the answer to the ith query.

Note:

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.

    Note that the trimmed number “02” is evaluated as 2.

Example 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 is 4 at index 3.

    There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3.

  2. Trimmed to the last 2 digits, nums is unchanged. The 2nd smallest number is 24.

Constraints:

Follow up: Could you use the Radix Sort Algorithm to solve this problem? What will be the complexity of that solution?

Solution

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public int[] smallestTrimmedNumbers(String[] nums, int[][] queries) {
        int numberOfDigits = nums[0].length();
        int placeValue = numberOfDigits;
        List<int[]> list = new ArrayList<>(numberOfDigits);
        while (--placeValue >= 0) {
            countSort(nums, placeValue, numberOfDigits, list);
        }
        int[] op = new int[queries.length];
        int i = 0;
        for (int[] query : queries) {
            int listIndex = query[1] - 1;
            int place = query[0] - 1;
            op[i++] = list.get(listIndex)[place];
        }
        return op;
    }

    private void countSort(String[] arr, int exp, int numberOfDigits, List<int[]> list) {
        int n = arr.length;
        String[] output = new String[n];
        int i;
        int[] count = new int[10];
        Arrays.fill(count, 0);
        // Store count of occurrences in count[]
        for (i = 0; i < n; i++) {
            int digit = arr[i].charAt(exp) - '0';
            count[digit]++;
        }
        for (i = 1; i < 10; i++) {
            count[i] += count[i - 1];
        }
        // Build the output array
        int[] op = new int[n];
        for (i = n - 1; i >= 0; i--) {
            int digit = arr[i].charAt(exp) - '0';
            int place = count[digit] - 1;
            output[place] = arr[i];
            if (exp == numberOfDigits - 1) {
                op[place] = i;
            } else {
                op[place] = list.get(list.size() - 1)[i];
            }
            count[digit]--;
        }
        list.add(op);
        System.arraycopy(output, 0, arr, 0, n);
    }
}