Skip to content

2343. Query Kth Smallest Trimmed Number 👎

  • Time: $O(q \cdot \texttt{sort})$
  • Space: $O(q + n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
 public:
  vector<int> smallestTrimmedNumbers(vector<string>& nums,
                                     vector<vector<int>>& queries) {
    vector<int> ans;
    for (const vector<int>& query : queries)
      ans.push_back(getKSmallestIndex(nums, query[0], query[1]));
    return ans;
  }

 private:
  int getKSmallestIndex(const vector<string>& nums, int k, int trim) {
    const int startIndex = nums[0].length() - trim;
    vector<pair<string, int>> trimmed;  // (num, index)

    for (int i = 0; i < nums.size(); ++i) {
      const string& num = nums[i];
      trimmed.push_back({num.substr(startIndex), i});
    }

    ranges::sort(trimmed);
    return trimmed[k - 1].second;
  }
};