File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 912. Sort an Array
3
+ * https://leetcode.com/problems/sort-an-array/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an array of integers nums, sort the array in ascending order and return it.
7
+ *
8
+ * You must solve the problem without using any built-in functions in O(nlog(n)) time
9
+ * complexity and with the smallest space complexity possible.
10
+ */
11
+
12
+ /**
13
+ * @param {number[] } nums
14
+ * @return {number[] }
15
+ */
16
+ var sortArray = function ( nums ) {
17
+ if ( nums . length <= 1 ) {
18
+ return nums ;
19
+ }
20
+
21
+ const min = Math . min ( ...nums ) ;
22
+ const max = Math . max ( ...nums ) ;
23
+ const count = new Array ( max - min + 1 ) . fill ( 0 ) ;
24
+
25
+ nums . forEach ( n => count [ n - min ] ++ ) ;
26
+
27
+ for ( let i = 0 , j = 0 ; i < count . length ; i ++ ) {
28
+ while ( count [ i ] -- > 0 ) {
29
+ nums [ j ++ ] = i + min ;
30
+ }
31
+ }
32
+
33
+ return nums ;
34
+ } ;
Original file line number Diff line number Diff line change 252
252
890|[ Find and Replace Pattern] ( ./0890-find-and-replace-pattern.js ) |Medium|
253
253
901|[ Online Stock Span] ( ./0901-online-stock-span.js ) |Medium|
254
254
905|[ Sort Array By Parity] ( ./0905-sort-array-by-parity.js ) |Easy|
255
+ 912|[ Sort an Array] ( ./0912-sort-an-array.js ) |Medium|
255
256
914|[ X of a Kind in a Deck of Cards] ( ./0914-x-of-a-kind-in-a-deck-of-cards.js ) |Medium|
256
257
916|[ Word Subsets] ( ./0916-word-subsets.js ) |Medium|
257
258
922|[ Sort Array By Parity II] ( ./0922-sort-array-by-parity-ii.js ) |Easy|
You can’t perform that action at this time.
0 commit comments