File tree 5 files changed +99
-0
lines changed
kth-smallest-element-in-a-bst
5 files changed +99
-0
lines changed Original file line number Diff line number Diff line change
1
+ // TC : O(n) | SC : O(n)
2
+
3
+ function containsDuplicate ( nums ) {
4
+ let original_length = nums . length ;
5
+ let modified_length = new Set ( nums ) . size ;
6
+ return original_length !== modified_length ;
7
+ }
Original file line number Diff line number Diff line change
1
+ // TC : O(n log n) | SC : O(n)
2
+
3
+ let findAllValuesInTree = ( root , obj ) => {
4
+ obj [ root . val ] = true ;
5
+ if ( ! root . left && ! root . right ) return obj ;
6
+ if ( root . left ) findAllValuesInTree ( root . left , obj ) ;
7
+ if ( root . right ) findAllValuesInTree ( root . right , obj ) ;
8
+
9
+ return obj ;
10
+ } ;
11
+
12
+ var kthSmallest = function ( root , k ) {
13
+ const obj = findAllValuesInTree ( root , { } ) ;
14
+ const sortedList = Object . keys ( obj )
15
+ . map ( Number )
16
+ . sort ( ( a , b ) => a - b ) ;
17
+
18
+ return sortedList [ k - 1 ] ;
19
+ } ;
Original file line number Diff line number Diff line change
1
+ let hammingWeight = function ( n ) {
2
+ return DecToBinAndGetSetBits ( n ) ;
3
+ } ;
4
+
5
+ // TC : O(log n) | SC : O(1)
6
+
7
+ let DecToBinAndGetSetBits = ( n ) => {
8
+ let targetNum = n ;
9
+ let result = 0 ;
10
+ while ( targetNum > 0 ) {
11
+ let remainder = targetNum % 2 ;
12
+ if ( remainder === 1 ) result += 1 ;
13
+ targetNum = parseInt ( targetNum / 2 ) ;
14
+ }
15
+ return result ;
16
+ } ;
17
+
18
+ // TC : O(log n) | SC : O(log n)
19
+ // 근데 사실 split 메서드 자체는 o(n)인데, toString과정을 통해 log(n)의 개수만큼 나와버린 것이면 o(log n)이라고 표기해도 되는걸까?
20
+
21
+ // let DecToBinAndGetSetBits = (n) => {
22
+ // let target = n;
23
+ // let bin = n.toString(2);
24
+ // return bin.split("").filter((el) => el == 1).length
25
+ // }
26
+
27
+ // TC : O(log n) | SC : O(log n)
28
+
29
+ // let DecToBinAndGetSetBits = (n) => {
30
+ // let target = n;
31
+ // let bin = n.toString(2);
32
+ // let matches = bin.match(/1/g);
33
+ // return matches.length
34
+ // }
Original file line number Diff line number Diff line change
1
+ var countSubstrings = function ( s ) {
2
+ let result = 0 ;
3
+ // 개수를 키워나가며, 각 자리가 대칭을 이루는지 검사한다.
4
+
5
+ // substring의 개수 설정
6
+ for ( let i = 0 ; i < s . length ; i ++ ) {
7
+ // 시작점 설정
8
+ for ( let j = 0 ; j < s . length - i ; j ++ ) {
9
+ let isPalindromic = true ;
10
+ // 대칭되는 요소를 하나씩 비교
11
+ for ( let k = j ; k < Math . ceil ( ( j * 2 + i ) / 2 ) ; k ++ ) {
12
+ if ( s [ k ] !== s [ j * 2 + i - k ] ) {
13
+ isPalindromic = false ;
14
+ break ;
15
+ }
16
+ }
17
+ if ( isPalindromic ) result += 1 ;
18
+ }
19
+ }
20
+
21
+ return result ;
22
+ } ;
23
+
24
+ // TC : o(n^3) | SC : o(1)
Original file line number Diff line number Diff line change
1
+ // TC : o(n log n) | SC : o(n)
2
+
3
+ var topKFrequent = function ( nums , k ) {
4
+ const elements = countElments ( nums ) ;
5
+ const keys = Object . keys ( elements ) . sort ( ( a , b ) => elements [ b ] - elements [ a ] ) ;
6
+ return keys . slice ( 0 , k ) ;
7
+ } ;
8
+
9
+ let countElments = ( nums ) => {
10
+ const temp = { } ;
11
+ for ( const num of nums ) {
12
+ temp [ num ] = ( count [ num ] || 0 ) + 1 ;
13
+ }
14
+ return temp ;
15
+ } ;
You can’t perform that action at this time.
0 commit comments