File tree 2 files changed +33
-46
lines changed
longest-common-subsequence
longest-consecutive-sequence
2 files changed +33
-46
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
4
+ * - ์ค๋ณต ์ซ์ ์ ๊ฑฐํ๊ณ ๋น ๋ฅด๊ฒ ์กฐํํ๊ธฐ ์ํด์ Set ์ฌ์ฉ
5
+ * - ์ฐ์๋ ์ซ์์ ์์์ (ํ์ฌ ์ซ์๋ณด๋ค ์์ ์ซ์๊ฐ ์๋ ๊ฒฝ์ฐ)๋ฅผ ์ฐพ์์ ์ฐ์๋ ๊ธธ์ด ์นด์ดํธ
6
+ * - ์ต๋ ๊ธธ์ด ๊ฐฑ์
7
+ *
8
+ * ์๊ฐ๋ณต์ก๋ : O(n)
9
+ * - Set ์์ฑ, ์ซ์ ํ์ ๋ชจ๋ O(n)
10
+ *
11
+ * ๊ณต๊ฐ๋ณต์ก๋ : O(n)
12
+ * - ์ต์
์ ๊ฒฝ์ฐ, nums ๋ฐฐ์ด ํฌ๊ธฐ๋งํผ set์ ์ ์ฅํ๋๊น O(n)
13
+ */
14
+ function longestConsecutive ( nums : number [ ] ) : number {
15
+ const numSet = new Set ( nums ) ;
16
+ let longestLength = 0 ;
17
+
18
+ for ( const num of numSet ) {
19
+ if ( ! numSet . has ( num - 1 ) ) {
20
+ let currentLength = 1 ;
21
+ let currentNum = num + 1 ;
22
+
23
+ while ( numSet . has ( currentNum ) ) {
24
+ currentLength ++ ;
25
+ currentNum ++ ;
26
+ }
27
+
28
+ longestLength = Math . max ( longestLength , currentLength ) ;
29
+ }
30
+ }
31
+
32
+ return longestLength ;
33
+ }
You canโt perform that action at this time.
0 commit comments