2422. Query Kth Smallest Trimmed Number
My accepted Python solution to LeetCode problem 2422, Query Kth Smallest Trimmed Number, running in 433ms.
- Difficulty: Medium
- Python
- Runtime 433ms
- Memory 17.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 433ms, memory 17.5MB, accepted 2025-12-29.
class Solution:
def smallestTrimmedNumbers(self, nums: List[str], queries: List[List[int]]) -> List[int]:
result = []
for k, trim in queries:
# Trim each number to rightmost 'trim' digits
trimmed = [(num[-trim:], i) for i, num in enumerate(nums)]
# Sort by trimmed value (string comparison works for equal length)
trimmed.sort(key=lambda x: (x[0], x[1]))
# Get k-th smallest (1-indexed)
result.append(trimmed[k-1][1])
return result