Email or username:

Password:

Forgot your password?
3 comments
Atanu 🇵🇸

@toby3d
class Solution {
public:
vector<int> targetIndices(vector<int>& nums, int target) {
vector<int> a;

int n = 0;
int l = 0;
for (auto i : nums) {
if (i < target) {
l++;
} else if (i == target) {
n++;
}
}
for (int i = 0; i < n; i++) {
a.push_back(l + i);
}
return a;
}
};

Maxim Lebedev

@atanu I use built-in "slices" and "sort" packages and saved on allocation by specifying capacity.

It is especially interesting that the solution using the construct `target < nums[i]` is 3ms slower but 0.04mb cheaper then `nums[i] != target`. I don't really understand why this is so.

Atanu 🇵🇸

@toby3d

I provided a solution in C++ that solves the problem in 0ms, try it!

The first observation is that there is no reason to pass over the data
twice. The classic solution sorts the data in the first pass and then
counts the number of targets indexes in the second pass.

Go Up