LeetCode 2343: Query Kth Smallest Trimmed Number Solution

Master LeetCode problem 2343 (Query Kth Smallest Trimmed Number), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation:

To solve this problem efficiently, we can follow these steps:

  1. Iterate through each query.
  2. Trim each number in nums to its rightmost trimi digits.
  3. Create a list of trimmed numbers with their original indices.
  4. Sort the list of trimmed numbers.
  5. Find the kth smallest trimmed number and return its original index.

Using Radix Sort can help us sort the trimmed numbers efficiently. The time complexity of this approach is O(N * M), where N is the number of elements and M is the maximum length of the elements in nums.

Solution Code

import java.util.*;

class Solution {
    public int[] kthSmallestTrimmedNumber(String[] nums, int[][] queries) {
        int[] answer = new int[queries.length];

        for (int i = 0; i < queries.length; i++) {
            int k = queries[i][0];
            int trim = queries[i][1];
            List<int[]> trimmedNums = new ArrayList<>();

            for (int j = 0; j < nums.length; j++) {
                int trimmed = Integer.parseInt(nums[j].substring(nums[j].length() - trim));
                trimmedNums.add(new int[]{trimmed, j});
            }

            Collections.sort(trimmedNums, (a, b) -> a[0] - b[0]);
            answer[i] = trimmedNums.get(k - 1)[1];
        }

        return answer;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2343 (Query Kth Smallest Trimmed Number)?

This page provides optimized solutions for LeetCode problem 2343 (Query Kth Smallest Trimmed Number) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2343 (Query Kth Smallest Trimmed Number)?

The time complexity for LeetCode 2343 (Query Kth Smallest Trimmed Number) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2343 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2343 in Java, C++, or Python.

Back to LeetCode Solutions