Skip to content

Commit bbf4080

Browse files
committed
1주차 문제 풀이 1개 추가
- Longest Consecutive Sequence
1 parent dfcbf61 commit bbf4080

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
int longestConsecutive(vector<int>& nums) {
4+
if (nums.empty())
5+
{
6+
return 0;
7+
}
8+
9+
unordered_set<int> s;
10+
s.reserve(nums.size());
11+
s.max_load_factor(0.4f);
12+
s.insert(nums.begin(), nums.end());
13+
14+
int ans = 0;
15+
for(auto& e : s)
16+
{
17+
if (s.contains(e - 1))
18+
{
19+
continue;
20+
}
21+
22+
int con = 1;
23+
int num = e;
24+
while (true)
25+
{
26+
num++;
27+
if(!s.contains(num))
28+
{
29+
break;
30+
}
31+
32+
con++;
33+
}
34+
ans = max(ans, con);
35+
}
36+
37+
return ans;
38+
}
39+
};

0 commit comments

Comments
 (0)