Skip to content

Commit cc53b06

Browse files
authored
Update how-many-numbers-are-smaller-than-the-current-number.cpp
1 parent 879fe8a commit cc53b06

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

C++/how-many-numbers-are-smaller-than-the-current-number.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,19 @@ class Solution {
2525
return count;
2626
}
2727
};
28+
29+
// Time: O(nlogn)
30+
// Space: O(n)
31+
class Solution2 {
32+
public:
33+
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
34+
vector<int> sorted_nums(nums);
35+
sort(begin(sorted_nums), end(sorted_nums));
36+
vector<int> result;
37+
for (const auto& i : nums) {
38+
result.emplace_back(distance(cbegin(sorted_nums),
39+
lower_bound(cbegin(sorted_nums), cend(sorted_nums), i)));
40+
}
41+
return result;
42+
}
43+
};

0 commit comments

Comments
 (0)