Query Kth Smallest Trimmed Number LeetCode Solution

Problem – Query Kth Smallest Trimmed Number LeetCode Solution

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.

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:

  • 1 <= nums.length <= 100
  • 1 <= nums[i].length <= 100
  • nums[i] consists of only digits.
  • All nums[i].length are equal.
  • 1 <= queries.length <= 100
  • queries[i].length == 2
  • 1 <= ki <= nums.length
  • 1 <= trimi <= nums[i].length

Query Kth Smallest Trimmed Number LeetCode Solution in C++

class Solution {
public:
    vector<int> smallestTrimmedNumbers(vector<string>& nums, vector<vector<int>>& queries) {
        vector<int> res;
        for(auto x:queries)
        {
            priority_queue<pair<string,int>> v;
            for(int i=0;i<nums.size();i++)
            {
                int t=nums[i].length()-x[1];
                string p=nums[i].substr(t,x[1]);
                if(v.size()<x[0])
                    v.push({p,i});
                else
                {
                    if(v.top().first > p)
                    {
                        v.pop();
                        v.push({p,i});
                    }
                }
            }
            int val=v.top().second;
            res.push_back(val);
        }
        return res;
    }
};

Query Kth Smallest Trimmed Number LeetCode Solution in Java

public int[] smallestTrimmedNumbers(String[] nums, int[][] queries) {
    HashMap<Integer, Node[]> map = new HashMap<>();
    int[] res = new int[queries.length];
    int idx = 0, len = nums[0].length();
    for(int[] query : queries){
        if(!map.containsKey(query[1])){  // make and store array if not already made.
            Node[] arr = new Node[nums.length];
            for(int i=0; i<nums.length; i++){
                String x = nums[i].substring(len-query[1], len); // trim as required
                arr[i] = new Node(i, x);
            }
			Arrays.sort(arr, (a, b)-> a.val.compareTo(b.val)); // sort array
            map.put(query[1], arr);
        }
        res[idx++] = map.get(query[1])[query[0]-1].index; // get required value.
    }
    return res;
}

class Node{ int index; String val;  // custom object to store both index and value.
    Node(int i, String v){ this.index = i; this.val = v; } }

Query Kth Smallest Trimmed Number LeetCode Solution in Python

def smallestTrimmedNumbers(A, Q): 
    m, n = len(A[0]), len(A)
    d = [list(range(n))]
    for i in range(m-1,-1,-1):
        rk = {x:j for j,x in enumerate(d[-1])}
        d.append(sorted(range(n), key=lambda x:(A[x][i],rk[x])))
    return [d[t][k-1] for k,t in Q]
Query Kth Smallest Trimmed Number LeetCode Solution Review:

In our experience, we suggest you solve this Query Kth Smallest Trimmed Number LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Query Kth Smallest Trimmed Number LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Query Kth Smallest Trimmed Number LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *