File tree 5 files changed +131
-0
lines changed
5 files changed +131
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n
3
+ * @return {number[] }
4
+ */
5
+ var countBits = function ( n ) {
6
+ const result = new Array ( n + 1 ) . fill ( 0 ) ;
7
+
8
+ for ( let i = 1 ; i <= n ; i ++ ) {
9
+ /** The number of 1's in i divided by 2, except last bit of i */
10
+ const totalOnes = result [ i >> 1 ] ;
11
+ const lastBit = i & 1 ;
12
+
13
+ result [ i ] = totalOnes + lastBit ;
14
+ }
15
+
16
+ return result ;
17
+ } ;
18
+
19
+ /**
20
+ * Time Complexity: O(n), where n is number input
21
+ * Reason: The algorithm processes each number from 1 to n exactly once.
22
+ *
23
+ * Space Complexity: O(n), where n is number input
24
+ * Reason: The algorithm uses an array of size n + 1
25
+ */
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } strList
3
+ * @return {string[][] }
4
+ */
5
+ var groupAnagrams = function ( strList ) {
6
+ const map = new Map ( ) ;
7
+
8
+ for ( const str of strList ) {
9
+ const sortedStr = str . split ( "" ) . sort ( ) . join ( "" ) ;
10
+
11
+ if ( ! map . has ( sortedStr ) ) {
12
+ map . set ( sortedStr , [ ] ) ;
13
+ }
14
+
15
+ map . get ( sortedStr ) . push ( str ) ;
16
+ }
17
+
18
+ return Array . from ( map . values ( ) ) ;
19
+ } ;
20
+
21
+ /**
22
+ * Time Complexity: O(n * k log k)
23
+ * Reason:
24
+ * - n is the number of strings in the input array.
25
+ * - k is the maximum length of a string in the input array.
26
+ * - Sorting each string takes O(k log k) time.
27
+ * - Thus, sorting all n strings takes O(n * k log k) time.
28
+ *
29
+ * Space Complexity: O(n * k)
30
+ * Reason:
31
+ * - We use a map to store the grouped anagrams.
32
+ * - In the worst case, we might store all n strings in the map.
33
+ * - Each string has a maximum length of k.
34
+ * - Thus, the space complexity is O(n * k).
35
+ */
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number }
4
+ */
5
+ var missingNumber = function ( nums ) {
6
+ const expectedSum = ( nums . length * ( nums . length + 1 ) ) / 2 ;
7
+ const actualSum = nums . reduce ( ( prev , curr ) => prev + curr ) ;
8
+
9
+ return expectedSum - actualSum ;
10
+ } ;
11
+
12
+ /**
13
+ * Time Complexity: O(n)
14
+ * Reason:
15
+ * - Finding the maximum number in the array takes O(n) time.
16
+ * - Calculating the target sum takes O(1) time.
17
+ * - Iterating through the array to update the target number takes O(n) time.
18
+ * - Checking the condition and returning the result takes O(1) time.
19
+ * - Therefore, the overall time complexity is O(n).
20
+ *
21
+ * Space Complexity: O(1)
22
+ * Reason:
23
+ * - The algorithm uses a constant amount of extra space (variables like maxNumber, hasZero, targetNumber).
24
+ * - No additional space proportional to the input size is used.
25
+ * - Therefore, the overall space complexity is O(1).
26
+ */
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n, maximum 32 bit number
3
+ * @return {number }
4
+ */
5
+ var hammingWeight = function ( n ) {
6
+ return n . toString ( 2 ) . replace ( / 0 / g, "" ) . length ;
7
+ } ;
8
+
9
+ /**
10
+ * Time Complexity: O(1)
11
+ * Reason:
12
+ * - n.toString(2): Converts the number to a binary string, which has a fixed length of up to 32 bits. This is O(1) because the length is constant.
13
+ * - replace(/0/g, ''): Removes all '0's from the binary string. The operation is O(1) since the string length is at most 32 characters.
14
+ * - .length: Getting the length of the resulting string is O(1).
15
+ * Therefore, the overall time complexity is O(1).
16
+ *
17
+ * Space Complexity: O(1)
18
+ * Reason:
19
+ * - n.toString(2): The binary string representation has a fixed maximum length of 32 characters, which requires O(1) space.
20
+ * - replace(/0/g, ''): The resulting string after removing '0's has a length of at most 32 characters, so it also requires O(1) space.
21
+ * - .length: Retrieving the length of a string does not require additional space.
22
+ * Therefore, the overall space complexity is O(1).
23
+ */
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n - a positive integer
3
+ * @return {number } - a positive integer
4
+ */
5
+ var reverseBits = function ( n ) {
6
+ const binary = n . toString ( 2 ) . padStart ( 32 , "0" ) ;
7
+ const reversedBinary = binary . split ( "" ) . reverse ( ) . join ( "" ) ;
8
+
9
+ return parseInt ( reversedBinary , 2 ) ;
10
+ } ;
11
+
12
+ /**
13
+ * Time Complexity: O(1)
14
+ * Reason:
15
+ * - Each step involves operations on fixed-length strings or arrays (maximum length of 32).
16
+ * - Thus, the time complexity for each operation is constant, resulting in an overall time complexity of O(1).
17
+ *
18
+ * Space Complexity: O(1)
19
+ * Reason:
20
+ * - The algorithm uses a constant amount of space for the binary string, reversed binary string, and intermediate arrays.
21
+ * - Each of these has a fixed length of 32, so the overall space complexity is O(1).
22
+ */
You can’t perform that action at this time.
0 commit comments