File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ [๋ฌธ์ ํ์ด]
3+ time: O(N), space: O(N)
4+ - ์ค๋ณต ์ ๊ฑฐ
5+ - ์ฐ์๋ ํ์ max ๊ตฌํ๊ธฐ
6+ -- ์ฐ์๋ ์ซ์ ๊ทธ๋ฃน ์ค ์ฒซ๋ฒ์งธ๋ถํฐ ์์๋๋๋ก
7+
8+ [ํ๊ณ ]
9+ ์ฐ์๋ ์ซ์ ๊ทธ๋ฃน ์ค ์ฒซ๋ฒ์งธ๋ถํฐ ์์๋๋๋ก ํ๋ ๋ถ๋ถ์์ ๋งํ๋ค..
10+ ์ฐจ๋ถํ ์๊ฐํด๋ณด๋ฉด ๋ฌด๋ฆฌ์์ด ํ ์ ์์ง ์์์๊น..
11+ */
12+ class Solution {
13+ public int longestConsecutive (int [] nums ) {
14+ if (nums .length == 0 ) {
15+ return 0 ;
16+ }
17+
18+ Set <Integer > numsSet = new HashSet <>();
19+ for (int num : nums ) {
20+ numsSet .add (num );
21+ }
22+
23+ int maxCount = 1 ;
24+ for (int num : numsSet ) {
25+ if (!numsSet .contains (num - 1 )) {
26+ int count = 1 ;
27+ int currentNum = num ;
28+ while (numsSet .contains (currentNum + 1 )) {
29+ count ++;
30+ currentNum ++;
31+ }
32+ maxCount = Math .max (maxCount , count );
33+ }
34+ }
35+ return maxCount ;
36+ }
37+ }
You canโt perform that action at this time.
0 commit comments