We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fe5c45a commit 104fe30Copy full SHA for 104fe30
0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp
@@ -0,0 +1,26 @@
1
+class Solution {
2
+public:
3
+ int longestConsecutive(vector<int>& nums) {
4
+ if(nums.empty()){
5
+ return 0;
6
+ }
7
+ sort(nums.begin(),nums.end());
8
+ vector<int> answer(nums.size(),1);
9
+ unordered_map<int,int> hashmap;
10
+ hashmap[nums[0]]=0;
11
+ for(int i=1;i<nums.size();i++){
12
+ hashmap[nums[i]]=i;
13
+ if(hashmap.count(nums[i]-1)>0){
14
+ int temp=hashmap[nums[i]-1];
15
+ answer[i]=max(answer[temp]+1,answer[i-1]);
16
17
18
+ int maximum=INT_MIN;
19
+ for(int i=0;i<answer.size();i++){
20
+ if(answer[i]>maximum){
21
+ maximum=answer[i];
22
23
24
+ return maximum;
25
26
+};
0 commit comments