File tree Expand file tree Collapse file tree 3 files changed +101
-0
lines changed
solution/1300-1399/1365.How Many Numbers Are Smaller Than the Current Number Expand file tree Collapse file tree 3 files changed +101
-0
lines changed Original file line number Diff line number Diff line change @@ -204,6 +204,49 @@ func smallerNumbersThanCurrent(nums []int) (ans []int) {
204
204
}
205
205
```
206
206
207
+ ### ** TypeScript**
208
+
209
+ ``` ts
210
+ function smallerNumbersThanCurrent(nums : number []): number [] {
211
+ const search = (nums : number [], x : number ) => {
212
+ let l = 0 ,
213
+ r = nums .length ;
214
+ while (l < r ) {
215
+ const mid = (l + r ) >> 1 ;
216
+ if (nums [mid ] >= x ) {
217
+ r = mid ;
218
+ } else {
219
+ l = mid + 1 ;
220
+ }
221
+ }
222
+ return l ;
223
+ };
224
+ const arr = nums .slice ().sort ((a , b ) => a - b );
225
+ for (let i = 0 ; i < nums .length ; ++ i ) {
226
+ nums [i ] = search (arr , nums [i ]);
227
+ }
228
+ return nums ;
229
+ }
230
+ ```
231
+
232
+ ``` ts
233
+ function smallerNumbersThanCurrent(nums : number []): number [] {
234
+ const cnt: number [] = new Array (102 ).fill (0 );
235
+ for (const x of nums ) {
236
+ ++ cnt [x + 1 ];
237
+ }
238
+ for (let i = 1 ; i < cnt .length ; ++ i ) {
239
+ cnt [i ] += cnt [i - 1 ];
240
+ }
241
+ const n = nums .length ;
242
+ const ans: number [] = new Array (n );
243
+ for (let i = 0 ; i < n ; ++ i ) {
244
+ ans [i ] = cnt [nums [i ]];
245
+ }
246
+ return ans ;
247
+ }
248
+ ```
249
+
207
250
### ** ...**
208
251
209
252
```
Original file line number Diff line number Diff line change @@ -181,6 +181,49 @@ func smallerNumbersThanCurrent(nums []int) (ans []int) {
181
181
}
182
182
```
183
183
184
+ ### ** TypeScript**
185
+
186
+ ``` ts
187
+ function smallerNumbersThanCurrent(nums : number []): number [] {
188
+ const search = (nums : number [], x : number ) => {
189
+ let l = 0 ,
190
+ r = nums .length ;
191
+ while (l < r ) {
192
+ const mid = (l + r ) >> 1 ;
193
+ if (nums [mid ] >= x ) {
194
+ r = mid ;
195
+ } else {
196
+ l = mid + 1 ;
197
+ }
198
+ }
199
+ return l ;
200
+ };
201
+ const arr = nums .slice ().sort ((a , b ) => a - b );
202
+ for (let i = 0 ; i < nums .length ; ++ i ) {
203
+ nums [i ] = search (arr , nums [i ]);
204
+ }
205
+ return nums ;
206
+ }
207
+ ```
208
+
209
+ ``` ts
210
+ function smallerNumbersThanCurrent(nums : number []): number [] {
211
+ const cnt: number [] = new Array (102 ).fill (0 );
212
+ for (const x of nums ) {
213
+ ++ cnt [x + 1 ];
214
+ }
215
+ for (let i = 1 ; i < cnt .length ; ++ i ) {
216
+ cnt [i ] += cnt [i - 1 ];
217
+ }
218
+ const n = nums .length ;
219
+ const ans: number [] = new Array (n );
220
+ for (let i = 0 ; i < n ; ++ i ) {
221
+ ans [i ] = cnt [nums [i ]];
222
+ }
223
+ return ans ;
224
+ }
225
+ ```
226
+
184
227
### ** ...**
185
228
186
229
```
Original file line number Diff line number Diff line change
1
+ function smallerNumbersThanCurrent ( nums : number [ ] ) : number [ ] {
2
+ const cnt : number [ ] = new Array ( 102 ) . fill ( 0 ) ;
3
+ for ( const x of nums ) {
4
+ ++ cnt [ x + 1 ] ;
5
+ }
6
+ for ( let i = 1 ; i < cnt . length ; ++ i ) {
7
+ cnt [ i ] += cnt [ i - 1 ] ;
8
+ }
9
+ const n = nums . length ;
10
+ const ans : number [ ] = new Array ( n ) ;
11
+ for ( let i = 0 ; i < n ; ++ i ) {
12
+ ans [ i ] = cnt [ nums [ i ] ] ;
13
+ }
14
+ return ans ;
15
+ }
You can’t perform that action at this time.
0 commit comments