forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path128. Longest Consecutive Sequence.cpp
115 lines (83 loc) · 2.64 KB
/
128. Longest Consecutive Sequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
Example:
Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
// TC : O(nlgn)
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int n=nums.size();
if(n==0) return 0;
sort(nums.begin(), nums.end());
int res=1, cnt=1;
for(int i=0;i<n-1;i++){
if(nums[i+1]==nums[i]) continue;
if(abs(nums[i+1]-nums[i]==1)){
cnt++;
} else cnt=1;
if(cnt>res) res=cnt;
}
return res;
}
};
/*
Brute Force
Considers each number in nums, attempting to count as high as possible from that number using only numbers in nums.
After it counts too high (i.e. currentNum refers to a number that nums does not contain),
it records the length of the sequence if it is larger than the current best.
*/
// TLE
class Solution {
bool arrayContains(vector<int> nums, int num) {
int n=nums.size();
for (int i = 0; i < n; i++) {
if (nums[i] == num) {
return true;
}
}
return false;
}
public:
int longestConsecutive(vector<int> &nums) {
int longestStreak = 0;
for (int num : nums) {
int currentNum = num;
int currentStreak = 1;
while (arrayContains(nums, currentNum + 1)) {
currentNum += 1;
currentStreak += 1;
}
longestStreak = max(longestStreak, currentStreak);
}
return longestStreak;
}
};
// O(N)
class Solution {
public:
int longestConsecutive(vector<int> &nums) {
int longestStreak = 0;
set<int> s;
// Hash all the array elements
for(int num: nums){
s.insert(num);
}
// check each possible sequence from the start then update optimal length
for (int num : nums) {
int currentNum = num;
int currentStreak = 1;
// if current element is the starting element of a sequence
if(s.find(currentNum-1)==s.end()){
// Then check for next elements in the sequence
while (s.find(currentNum+1)!=s.end()) {
currentNum += 1;
currentStreak += 1;
}
}
longestStreak = max(longestStreak, currentStreak);
}
return longestStreak;
}
};