Skip to content

Commit

Permalink
Create 2343.Query-Kth-Smallest-Trimmed-Number.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Jul 17, 2022
1 parent 635499d commit 535a03a
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public:
vector<int> smallestTrimmedNumbers(vector<string>& nums, vector<vector<int>>& queries)
{
int m = nums.size(), n = nums[0].size();
vector<vector<int>>ans(n+1, vector<int>(m));

for (int i=0; i<m; i++)
ans[0][i] = i;

for (int t=1; t<=n; t++)
{
vector<vector<int>>bucket(10);
for (int i=0; i<m; i++)
{
int idx = ans[t-1][i];
bucket[nums[idx][n-t]-'0'].push_back(idx);
}

int i = 0;
for (int b=0; b<10; b++)
for (int idx: bucket[b])
{
ans[t][i] = idx;
i++;
}
}

vector<int>rets;
for (auto& q: queries)
{
rets.push_back(ans[q[1]][q[0]-1]);
}

return rets;
}
};

0 comments on commit 535a03a

Please sign in to comment.