51
51
52
52
<!-- 这里可写通用的实现逻辑 -->
53
53
54
- 计数排序,求前缀和。
54
+ ** 方法一:排序 + 二分查找**
55
+
56
+ 我们可以将数组 $nums$ 复制一份,记为 $arr$,然后对 $arr$ 进行升序排序。
57
+
58
+ 接下来,对于 $nums$ 中的每个元素 $x$,我们可以通过二分查找的方法找到第一个大于等于 $x$ 的元素的下标 $j$,那么 $j$ 就是比 $x$ 小的元素的个数,我们将 $j$ 存入答案数组中即可。
59
+
60
+ 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
61
+
62
+ ** 方法二:计数排序 + 前缀和**
63
+
64
+ 我们注意到数组 $nums$ 中的元素的范围是 $[ 0, 100] $,因此我们可以使用计数排序的方法,先统计数组 $nums$ 中每个元素的个数。然后对计数数组进行前缀和计算,最后遍历数组 $nums$,对于每个元素 $x$,我们直接将计数数组中下标为 $x$ 的元素的值加入答案数组即可。
65
+
66
+ 时间复杂度 $O(n + M)$,空间复杂度 $O(M)$,其中 $n$ 和 $M$ 分别是数组 $nums$ 的长度和最大值。
55
67
56
68
<!-- tabs:start -->
57
69
62
74
``` python
63
75
class Solution :
64
76
def smallerNumbersThanCurrent (self , nums : List[int ]) -> List[int ]:
65
- cnt = [0 ] * 101
66
- for num in nums:
67
- cnt[num] += 1
68
- for i in range (1 , 101 ):
69
- cnt[i] += cnt[i - 1 ]
70
- res = []
71
- for num in nums:
72
- res.append(0 if num == 0 else cnt[num - 1 ])
73
- return res
77
+ arr = sorted (nums)
78
+ return [bisect_left(arr, x) for x in nums]
79
+ ```
80
+
81
+ ``` python
82
+ class Solution :
83
+ def smallerNumbersThanCurrent (self , nums : List[int ]) -> List[int ]:
84
+ cnt = [0 ] * 102
85
+ for x in nums:
86
+ cnt[x + 1 ] += 1
87
+ s = list (accumulate(cnt))
88
+ return [s[x] for x in nums]
74
89
```
75
90
76
91
### ** Java**
@@ -80,19 +95,112 @@ class Solution:
80
95
``` java
81
96
class Solution {
82
97
public int [] smallerNumbersThanCurrent (int [] nums ) {
83
- int [] cnt = new int [101 ];
84
- for (int e : nums) {
85
- ++ cnt[e];
98
+ int [] arr = nums. clone();
99
+ Arrays . sort(arr);
100
+ for (int i = 0 ; i < nums. length; ++ i) {
101
+ nums[i] = search(arr, nums[i]);
102
+ }
103
+ return nums;
104
+ }
105
+
106
+ private int search (int [] nums , int x ) {
107
+ int l = 0 , r = nums. length;
108
+ while (l < r) {
109
+ int mid = (l + r) >> 1 ;
110
+ if (nums[mid] >= x) {
111
+ r = mid;
112
+ } else {
113
+ l = mid + 1 ;
114
+ }
115
+ }
116
+ return l;
117
+ }
118
+ }
119
+ ```
120
+
121
+ ``` java
122
+ class Solution {
123
+ public int [] smallerNumbersThanCurrent (int [] nums ) {
124
+ int [] cnt = new int [102 ];
125
+ for (int x : nums) {
126
+ ++ cnt[x + 1 ];
86
127
}
87
- for (int i = 1 ; i < 101 ; ++ i) {
128
+ for (int i = 1 ; i < cnt . length ; ++ i) {
88
129
cnt[i] += cnt[i - 1 ];
89
130
}
90
- int [] res = new int [nums. length];
91
- for (int i = 0 ; i < nums. length; ++ i) {
92
- res[i] = nums[i] == 0 ? 0 : cnt[nums[i] - 1 ];
131
+ int n = nums. length;
132
+ int [] ans = new int [n];
133
+ for (int i = 0 ; i < n; ++ i) {
134
+ ans[i] = cnt[nums[i]];
135
+ }
136
+ return ans;
137
+ }
138
+ }
139
+ ```
140
+
141
+ ### ** C++**
142
+
143
+ ``` cpp
144
+ class Solution {
145
+ public:
146
+ vector<int > smallerNumbersThanCurrent(vector<int >& nums) {
147
+ vector<int > arr = nums;
148
+ sort(arr.begin(), arr.end());
149
+ for (int i = 0; i < nums.size(); ++i) {
150
+ nums[ i] = lower_bound(arr.begin(), arr.end(), nums[ i] ) - arr.begin();
151
+ }
152
+ return nums;
153
+ }
154
+ };
155
+ ```
156
+
157
+ ```cpp
158
+ class Solution {
159
+ public:
160
+ vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
161
+ int cnt[102]{};
162
+ for (int& x : nums) {
163
+ ++cnt[x + 1];
164
+ }
165
+ for (int i = 1; i < 102; ++i) {
166
+ cnt[i] += cnt[i - 1];
167
+ }
168
+ vector<int> ans;
169
+ for (int& x : nums) {
170
+ ans.push_back(cnt[x]);
93
171
}
94
- return res ;
172
+ return ans ;
95
173
}
174
+ };
175
+ ```
176
+
177
+ ### ** Go**
178
+
179
+ ``` go
180
+ func smallerNumbersThanCurrent (nums []int ) (ans []int ) {
181
+ arr := make ([]int , len (nums))
182
+ copy (arr, nums)
183
+ sort.Ints (arr)
184
+ for i , x := range nums {
185
+ nums[i] = sort.SearchInts (arr, x)
186
+ }
187
+ return nums
188
+ }
189
+ ```
190
+
191
+ ``` go
192
+ func smallerNumbersThanCurrent (nums []int ) (ans []int ) {
193
+ cnt := [102 ]int {}
194
+ for _ , x := range nums {
195
+ cnt[x+1 ]++
196
+ }
197
+ for i := 1 ; i < len (cnt); i++ {
198
+ cnt[i] += cnt[i-1 ]
199
+ }
200
+ for _ , x := range nums {
201
+ ans = append (ans, cnt[x])
202
+ }
203
+ return
96
204
}
97
205
```
98
206
0 commit comments