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