java.lang.Object
g2301_2400.s2343_query_kth_smallest_trimmed_number.Solution

public class Solution extends Object
2343 - Query Kth Smallest Trimmed Number.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> array of strings <code>nums</code>, where each string is of <strong>equal length</strong> and consists of only digits.</p> <p>You are also given a <strong>0-indexed</strong> 2D integer array <code>queries</code> where <code>queries[i] = [k<sub>i</sub>, trim<sub>i</sub>]</code>. For each <code>queries[i]</code>, you need to:</p> <ul> <li><strong>Trim</strong> each number in <code>nums</code> to its <strong>rightmost</strong> <code>trim<sub>i</sub></code> digits.</li> <li>Determine the <strong>index</strong> of the <code>k<sub>i</sub><sup>th</sup></code> smallest trimmed number in <code>nums</code>. If two trimmed numbers are equal, the number with the <strong>lower</strong> index is considered to be smaller.</li> <li>Reset each number in <code>nums</code> to its original length.</li> </ul> <p>Return <em>an array</em> <code>answer</code> <em>of the same length as</em> <code>queries</code>, <em>where</em> <code>answer[i]</code> <em>is the answer to the</em> <code>i<sup>th</sup></code> <em>query.</em></p> <p><strong>Note</strong>:</p> <ul> <li>To trim to the rightmost <code>x</code> digits means to keep removing the leftmost digit, until only <code>x</code> digits remain.</li> <li>Strings in <code>nums</code> may contain leading zeros.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [&ldquo;102&rdquo;,&ldquo;473&rdquo;,&ldquo;251&rdquo;,&ldquo;814&rdquo;], queries = [[1,1],[2,3],[4,2],[1,2]]</p> <p><strong>Output:</strong> [2,2,1,0]</p> <p><strong>Explanation:</strong></p> <ol> <li> <p>After trimming to the last digit, nums = [&ldquo;2&rdquo;,&ldquo;3&rdquo;,&ldquo;1&rdquo;,&ldquo;4&rdquo;]. The smallest number is 1 at index 2.</p> </li> <li> <p>Trimmed to the last 3 digits, nums is unchanged. The 2<sup>nd</sup> smallest number is 251 at index 2.</p> </li> <li> <p>Trimmed to the last 2 digits, nums = [&ldquo;02&rdquo;,&ldquo;73&rdquo;,&ldquo;51&rdquo;,&ldquo;14&rdquo;]. The 4<sup>th</sup> smallest number is 73.</p> </li> <li> <p>Trimmed to the last 2 digits, the smallest number is 2 at index 0.</p> <p>Note that the trimmed number &ldquo;02&rdquo; is evaluated as 2.</p> </li> </ol> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [&ldquo;24&rdquo;,&ldquo;37&rdquo;,&ldquo;96&rdquo;,&ldquo;04&rdquo;], queries = [[2,1],[2,2]]</p> <p><strong>Output:</strong> [3,0]</p> <p><strong>Explanation:</strong></p> <ol> <li> <p>Trimmed to the last digit, nums = [&ldquo;4&rdquo;,&ldquo;7&rdquo;,&ldquo;6&rdquo;,&ldquo;4&rdquo;]. The 2<sup>nd</sup> smallest number is 4 at index 3.</p> <p>There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3.</p> </li> <li> <p>Trimmed to the last 2 digits, nums is unchanged. The 2<sup>nd</sup> smallest number is 24.</p> </li> </ol> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 100</code></li> <li><code>1 <= nums[i].length <= 100</code></li> <li><code>nums[i]</code> consists of only digits.</li> <li>All <code>nums[i].length</code> are <strong>equal</strong>.</li> <li><code>1 <= queries.length <= 100</code></li> <li><code>queries[i].length == 2</code></li> <li><code>1 <= k<sub>i</sub> <= nums.length</code></li> <li><code>1 <= trim<sub>i</sub> <= nums[i].length</code></li> </ul> <p><strong>Follow up:</strong> Could you use the <strong>Radix Sort Algorithm</strong> to solve this problem? What will be the complexity of that solution?</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • smallestTrimmedNumbers

      public int[] smallestTrimmedNumbers(String[] nums, int[][] queries)