File tree Expand file tree Collapse file tree 5 files changed +180
-0
lines changed
longest-common-subsequence Expand file tree Collapse file tree 5 files changed +180
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {boolean }
4
+ */
5
+
6
+ /**
7
+ *
8
+ * ์ ๊ทผ ๋ฐฉ๋ฒ
9
+ * - Set ๊ฐ์ฒด ์ฌ์ฉํด์ ์ซ์ ์ค๋ณต ์ ๊ฑฐํ๊ธฐ
10
+ * - ์๋ณธ ๋ฐฐ์ด๊ณผ ๊ธธ์ด ๋น๊ตํ๊ธฐ
11
+ *
12
+ * ์๊ฐ๋ณต์ก๋ :
13
+ * - ๋ฐฐ์ด ์ํํด์ ์์ Set์ ์ฝ์
: O(n)
14
+ * - Set์ ์ฌ์ด์ฆ ํฌ๊ธฐ ๋น๊ต : O(1)
15
+ *
16
+ * ๊ณต๊ฐ๋ณต์ก๋ :
17
+ * - Set์ ์ ๋ํฌํ ์ซ์ ์ ์ฅ : O(n)
18
+ */
19
+
20
+ var containsDuplicate = function ( nums ) {
21
+ return new Set ( nums ) . size !== nums . length ;
22
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ * ์ ๊ทผ ๋ฐฉ๋ฒ : dp ์ฌ์ฉ
4
+ * - ๋ฐฐ์ด ๊ธธ์ด๊ฐ 1๊ฐ ์ผ๋๋ ์ฒซ ๋ฒ์จฐ ๊ฐ ๋ฆฌํดํ๊ณ , 2๊ฐ๋ฉด ๋ ํฐ ์๋ฅผ ๋ฆฌํดํ๋ค.
5
+ * - 3๊ฐ ๋ถํฐ๋ ๋ฐ๋ก ์์ง ๊ฐ์ ํ์ฌ์ง ๊ฐ์ ๋ํ ๊ฐ๊ณผ ์์์ง์ ๊ฐ์ ๋น๊ตํด์ ํฐ ๊ฐ์ ๊ณ์ฐํ๋ค.
6
+ * - ๋ค์ ์ง ๊ฐ์ ํ์ฌ๊น์ง์ ๊ฐ ํ์ฉํ๊ธฐ ์ํด์, ๋ฐ๋ก ์์ง, ์์์ง ๊ฐ์ ์
๋ฐ์ดํธํด์ค๋ค.
7
+ * - ํ์ฌ ๊ฐ์ด ์ ์ฅ๋ ์์ง๊ฐ์ ๋ฆฌํดํ๋ค.
8
+ *
9
+ * ์๊ฐ๋ณต์ก๋ :
10
+ * - ์ฃผ์ด์ง ์ซ์ ๋ฐฐ์ด ๊ธธ์ด๋งํผ 1ํ ์ํํ๋๊น O(n)
11
+ *
12
+ * ๊ณต๊ฐ๋ณต์ก๋ :
13
+ * - ์์ง๊ณผ ์์์ง ๊ฐ์ 2๊ฐ์ ๋ณ์์ ์ ์ฅํด์ผํ๋๊น O(1)
14
+ *
15
+ */
16
+ /**
17
+ * @param {number[] } nums
18
+ * @return {number }
19
+ */
20
+ var rob = function ( nums ) {
21
+ if ( nums . length === 1 ) return nums [ 0 ] ;
22
+ if ( nums . length === 2 ) return Math . max ( nums [ 0 ] , nums [ 1 ] ) ;
23
+
24
+ let prevPrevHouse = nums [ 0 ] ;
25
+ let prevHouse = Math . max ( nums [ 0 ] , nums [ 1 ] ) ;
26
+
27
+ for ( let i = 2 ; i < nums . length ; i ++ ) {
28
+ const current = Math . max ( prevHouse , prevPrevHouse + nums [ i ] ) ;
29
+ prevPrevHouse = prevHouse ;
30
+ prevHouse = current ;
31
+ }
32
+
33
+ return prevHouse ;
34
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
4
+ * - ์ค๋ณต ์ซ์ ์ ๊ฑฐํ ๋ค, ์ซ์ ์ํํ๋ฉด์ ์ฐ์ ์ซ์์ ์์ ์ง์ ์ธ์ง ์ฒดํฌ
5
+ * - ๋ ์์ ์ซ์๊ฐ ์์ผ๋ฉด ํ์ฌ ์ซ์๊ฐ ์ฐ์ ์ซ์์ ์์ ์ง์ ์ด๊ธฐ ๋๋ฌธ์, ์ฐ์๋ ๋ค์ ํฐ ์ซ์๊ฐ ์กด์ฌํ๋์ง ์ฒดํฌ
6
+ * - ์์ผ๋ฉด count ์ฆ๊ฐ์ํค๊ณ , ๊ทธ ๋ค์ ์ซ์ ์๋์ง ๋ฐ๋ณตํด์ ์ฒดํฌ
7
+ * - ์ฐ์ ์ซ์๊ฐ ์กด์ฌํ์ง ์์ ๋๊น์ง ์ํํ๊ธฐ
8
+ * - count๊ฐ maxCount๋ณด๋ค ํฐ ๊ฒฝ์ฐ maxCount๊ฐ์ count ๊ฐ์ผ๋ก ์
๋ฐ์ดํธ
9
+ *
10
+ * ์๊ฐ๋ณต์ก๋ :
11
+ * - ์ซ์ ๋ฐฐ์ด ๊ธธ์ด๋ฅผ ๋ชจ๋ ์ํํ๋๊น O(n)
12
+ *
13
+ * ๊ณต๊ฐ๋ณต์ก๋ :
14
+ * - Set์ ์ฌ์ฉํด์ ์ซ์ ์ค๋ณต ์ ๊ฑฐํ๊ณ ์ ์ฅํ๋๊น O(n)
15
+ */
16
+
17
+ /**
18
+ * @param {number[] } nums
19
+ * @return {number }
20
+ */
21
+ var longestConsecutive = function ( nums ) {
22
+ // ๋ฐฐ์ด ๋น์ด์๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
23
+ if ( nums . length === 0 ) return 0 ;
24
+
25
+ const uniqueNums = new Set ( nums ) ;
26
+ let maxCount = 1 ;
27
+
28
+ for ( const num of uniqueNums ) {
29
+ // ์ฐ์๋ ์ซ์์ ์์ ์ง์ ์ธ์ง ์ฒดํฌ(๋ ์์ ์ซ์๊ฐ ์กด์ฌํ์ง ์์์ผ ํจ)
30
+ if ( ! uniqueNums . has ( num - 1 ) ) {
31
+ let next = num + 1 ;
32
+ let count = 1 ;
33
+
34
+ // ์ฐ์ ์ซ์๊ฐ ๋ ์กด์ฌํ๋์ง ์ฒดํฌ
35
+ while ( uniqueNums . has ( next ) ) {
36
+ next ++ ;
37
+ count ++ ;
38
+ }
39
+
40
+ // ๊ธฐ์กด maxCount๋ณด๋ค ํฌ๋ฉด, count ๊ฐ์ผ๋ก ์
๋ฐ์ดํธํ๊ธฐ
41
+ if ( maxCount < count ) maxCount = count ;
42
+ }
43
+ }
44
+
45
+ return maxCount ;
46
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
3
+ * - ์ซ์ ๋ฐ์ด์ ์ ์ฒด ์ํํ๋ฉด์ ๋น๋์๋ฅผ ๊ฐ์ฒด์ ์ ์ฅ
4
+ * - ๊ฐ์ฒด ๊ฐ์ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ ๋ค, ์ํ๋ ํญ๋ชฉ๋งํผ ์๋ฅด๊ณ ์ซ์๋ก ๋ณํํ ๋ค ๋ฆฌํดํ๊ธฐ
5
+ *
6
+ * ์๊ฐ๋ณต์ก๋ : O(nlogn)
7
+ * - ์ซ์ ๋ฐฐ์ด ๊ธธ์ด = n , ๊ฐ์ ธ์ฌ ํญ๋ชฉ ๊ฐ์ = k
8
+ * - ๊ฐ์ฒด์ ์ซ์์ ๋น๋์ ์ ์ฅํ๊ธฐ ์ํด์ ๋ชจ๋ ์ซ์ ์ํ : O(n)
9
+ * - ๊ฐ์ฒด ํค๊ฐ์ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ : O(nlogn)
10
+ * - slice, map : O(k)
11
+ *
12
+ * ๊ณต๊ฐ๋ณต์ก๋ :
13
+ * - ์ซ์ ๋ฐฐ์ด์ ๊ธธ์ด๋งํผ ๊ฐ์ฒด์ ์ ์ฅํ๋๊น O(n)
14
+ */
15
+
16
+ /**
17
+ * @param {number[] } nums
18
+ * @param {number } k
19
+ * @return {number[] }
20
+ */
21
+
22
+ var topKFrequent = function ( nums , k ) {
23
+ const obj = { } ;
24
+
25
+ for ( const num of nums ) {
26
+ obj [ num ] = ( obj [ num ] ?? 0 ) + 1 ;
27
+ }
28
+
29
+ return Object . entries ( obj )
30
+ . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] )
31
+ . slice ( 0 , k )
32
+ . map ( ( item ) => Number ( item [ 0 ] ) ) ;
33
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
4
+ * - ๋ฌธ์์ด ์ํํ๋ฉด์ ์๋ฌธ์๋ก ๋ณ๊ฒฝํ ๋ค, ์ ๊ท์์ ์ด์ฉํด ์ํ๋ฒณ๊ณผ ์ซ์๋ง ์ถ์ถ
5
+ * - ์ถ์ถ๋ ๋ฌธ์์ ํฌ ํฌ์ธํฐ ์ฌ์ฉํด์ ์๋ค ๋ฌธ์ ๊ฐ์์ง ๋น๊ต.
6
+ * - ๋ค๋ฅด๋ฉด false ๋ฐ๋ก ๋ฆฌํดํ๊ณ , ๋๊น์ง ๊ฐ์ผ๋ฉด true ๋ฐํ
7
+ *
8
+ * ์๊ฐ๋ณต์ก๋ :
9
+ * - ๋ฌธ์์ด ๊ธธ์ด๊ฐ n์ผ ๋, O(n)
10
+ * - match๋ก ๋ฌธ์์ด ์ฒดํฌ : O(n)
11
+ * - ํฌ ํฌ์ธํฐ๋ก ์๋ค ๋ฌธ์ ๋น๊ต : O(n)
12
+ *
13
+ * ๊ณต๊ฐ๋ณต์ก๋ :
14
+ * - ์ต์
์ ๊ฒฝ์ฐ ๋ชจ๋ ๋ฌธ์๊ฐ ์ํ๋ฒณ์ด๊ฑฐ๋ ์ซ์์ธ ๊ฒฝ์ฐ ๊ธธ์ด๊ฐ n์ด ๋จ : O(n)
15
+ *
16
+ * ๋ฐฐ์ด ์ :
17
+ * - ๋ฌธ์๊ฐ ํ์ํ ๊ฒ ์๋๋๊น ๋ถํ์ํ ๋ฐฐ์ด ๋ณํ(reverse) ์ค์ด๊ธฐ
18
+ * - ๋ฌธ์์ด์ด ์กฐ๊ฑด ๋ฒ์์ ๋ถํฉํ๋์ง ์ฒดํฌํ ๋๋ ์ ๊ท์ ํ์ฉํ๊ธฐ
19
+ */
20
+
21
+ /**
22
+ * @param {string } s
23
+ * @return {boolean }
24
+ */
25
+
26
+ var isPalindrome = function ( s ) {
27
+ const alphanumericCharacters = s . toLowerCase ( ) . match ( / [ a - z 0 - 9 ] / g) || [ ] ;
28
+
29
+ let leftPointer = 0 ;
30
+ let rightPointer = alphanumericCharacters . length - 1 ;
31
+
32
+ while ( leftPointer < rightPointer ) {
33
+ if (
34
+ alphanumericCharacters [ leftPointer ] !==
35
+ alphanumericCharacters [ rightPointer ]
36
+ ) {
37
+ return false ;
38
+ }
39
+
40
+ leftPointer ++ ;
41
+ rightPointer -- ;
42
+ }
43
+
44
+ return true ;
45
+ } ;
You canโt perform that action at this time.
0 commit comments