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