File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
solution/3000-3099/3011.Find if Array Can Be Sorted Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {boolean }
4
+ */
5
+ var canSortArray = function ( nums ) {
6
+ let preMx = 0 ;
7
+ const n = nums . length ;
8
+ for ( let i = 0 ; i < n ; ) {
9
+ const cnt = bitCount ( nums [ i ] ) ;
10
+ let j = i + 1 ;
11
+ let [ mi , mx ] = [ nums [ i ] , nums [ i ] ] ;
12
+ while ( j < n && bitCount ( nums [ j ] ) === cnt ) {
13
+ mi = Math . min ( mi , nums [ j ] ) ;
14
+ mx = Math . max ( mx , nums [ j ] ) ;
15
+ j ++ ;
16
+ }
17
+ if ( preMx > mi ) {
18
+ return false ;
19
+ }
20
+ preMx = mx ;
21
+ i = j ;
22
+ }
23
+ return true ;
24
+ } ;
25
+
26
+ const bitCount = ( i ) => {
27
+ i = i - ( ( i >>> 1 ) & 0x55555555 ) ;
28
+ i = ( i & 0x33333333 ) + ( ( i >>> 2 ) & 0x33333333 ) ;
29
+ i = ( i + ( i >>> 4 ) ) & 0x0f0f0f0f ;
30
+ i = i + ( i >>> 8 ) ;
31
+ i = i + ( i >>> 16 ) ;
32
+ return i & 0x3f ;
33
+ }
You can’t perform that action at this time.
0 commit comments