File tree Expand file tree Collapse file tree 5 files changed +81
-0
lines changed Expand file tree Collapse file tree 5 files changed +81
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(n)
2
+ // Space Complexity: O(n)
3
+
4
+ var countBits = function ( n ) {
5
+ // initialize an array to hold the result.
6
+ let ans = new Array ( n + 1 ) . fill ( 0 ) ;
7
+
8
+ // iterate through all numbers from 1 to n.
9
+ for ( let i = 1 ; i <= n ; i ++ ) {
10
+ ans [ i ] = ans [ i >> 1 ] + ( i & 1 ) ;
11
+ }
12
+ return ans ;
13
+ } ;
Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(n * k)
2
+ // Space Complexity: O(n * k)
3
+
4
+ var groupAnagrams = function ( strs ) {
5
+ const map = new Map ( ) ;
6
+ for ( const str of strs ) {
7
+ // initialize an array to count the frequency of characters.
8
+ const count = new Array ( 26 ) . fill ( 0 ) ;
9
+ // increment the count for each character.
10
+ for ( const char of str ) {
11
+ count [ char . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ] ++ ;
12
+ }
13
+ // generate a unique key based on character frequencies.
14
+ const key = count . join ( '#' ) ;
15
+ // if the key exists, push the original string to its group.
16
+ if ( map . has ( key ) ) {
17
+ map . get ( key ) . push ( str ) ;
18
+ } else { // else, create a new group with the key.
19
+ map . set ( key , [ str ] ) ;
20
+ }
21
+ }
22
+
23
+ // return the groups of the map as an array.
24
+ return Array . from ( map . values ( ) ) ;
25
+ } ;
Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(n)
2
+ // Space Complexity: O(1)
3
+
4
+ var missingNumber = function ( nums ) {
5
+ const n = nums . length ;
6
+ // sum of numbers from 0 to n.
7
+ const expectedSum = ( n * ( n + 1 ) ) / 2 ;
8
+ // sum of numbers in the array.
9
+ const actualSum = nums . reduce ( ( acc , curr ) => acc + curr , 0 ) ;
10
+
11
+ // the difference is the missing number.
12
+ return expectedSum - actualSum ;
13
+ } ;
Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(log n)
2
+ // Space Complexity: O(1)
3
+
4
+ var hammingWeight = function ( n ) {
5
+ let count = 0 ;
6
+ while ( n !== 0 ) {
7
+ // add 1 to count if the last bit is 1.
8
+ count += n & 1 ;
9
+ // unsigned right shift to process the next bit.
10
+ n = n >>> 1 ;
11
+ }
12
+
13
+ return count ;
14
+ } ;
Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(1)
2
+ // Space Complexity: O(1)
3
+
4
+ var reverseBits = function ( n ) {
5
+ let result = 0 ;
6
+ // iterate 32 times since it's a 32-bit integer.
7
+ for ( let i = 0 ; i < 32 ; i ++ ) {
8
+ // shift the result to the left by 1 bit and OR it with the least significant bit of n.
9
+ result = ( result << 1 ) | ( n & 1 ) ;
10
+ // right shift n by 1 to consider the next bit.
11
+ n >>>= 1 ;
12
+ }
13
+
14
+ // convert to unsigned integer.
15
+ return result >>> 0 ;
16
+ } ;
You can’t perform that action at this time.
0 commit comments