File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * μκ° λ³΅μ‘λ:
3
+ * setμ μμ μ€ μ°μνλ μνμ€μ 첫λ²μ§Έ μ«μμΌ λλ§
4
+ * while 루νλ₯Ό μ€ν
5
+ * λ°λΌμ μμλΉ μ΅λ 1ν μν
6
+ * μ¦, μκ° λ³΅μ‘λλ O(n)
7
+ * κ³΅κ° λ³΅μ‘λ:
8
+ * setμ μ€λ³΅μ΄ μμ κ²½μ° μ΅λ O(n)λ₯Ό μ°¨μ§ν¨
9
+ * μ¦, κ³΅κ° λ³΅μ‘λλ O(n)
10
+ */
11
+ /**
12
+ * @param {number[] } nums
13
+ * @return {number }
14
+ */
15
+ var longestConsecutive = function ( nums ) {
16
+ const set = new Set ( nums ) ;
17
+ let res = 0 ;
18
+ for ( const n of set ) {
19
+ let seqLen = 0 , target = n ;
20
+ const isSeqStart = ! set . has ( target - 1 ) ;
21
+ if ( isSeqStart ) {
22
+ while ( set . has ( target ) ) {
23
+ target ++ ;
24
+ seqLen ++ ;
25
+ }
26
+ }
27
+ res = Math . max ( seqLen , res ) ;
28
+ }
29
+ return res ;
30
+ } ;
You canβt perform that action at this time.
0 commit comments