File tree Expand file tree Collapse file tree 5 files changed +99
-0
lines changed
kth-smallest-element-in-a-bst Expand file tree Collapse file tree 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