-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path128.cpp
More file actions
executable file
·147 lines (125 loc) · 3.3 KB
/
Copy path128.cpp
File metadata and controls
executable file
·147 lines (125 loc) · 3.3 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <algorithm>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
int longestConsecutive(vector<int> &nums) {
if (nums.size() > 1) {
unordered_set<int> s(nums.begin(), nums.end());
vector<int> uniq(s.begin(), s.end());
sort(uniq.begin(), uniq.end());
int current_streak = 1;
int longest_streak = 1;
int sz = uniq.size();
for (int i=0; i<sz-1; i++) {
if ((uniq[i+1] - uniq[i]) == 1) {
current_streak++;
if (current_streak >= longest_streak) {
longest_streak = max(longest_streak, current_streak);
}
}
else {
current_streak = 1;
}
}
return longest_streak;
}
else if (nums.size() == 1) {
return 1;
}
else {
return 0;
}
}
};
/* Brute Force
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int res = 0;
unordered_set<int> store(nums.begin(), nums.end());
for (int num : nums) {
int streak = 0, curr = num;
while (store.find(curr) != store.end()) {
streak++;
curr++;
}
res = max(res, streak);
}
return res;
}
};
Time & Space Complexity
Time complexity: O(n^2)
Space complexity: O(n)
*/
/* Sorting
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if (nums.empty()) return 0;
sort(nums.begin(), nums.end());
int res = 0, curr = nums[0], streak = 0, i = 0;
while (i < nums.size()) {
if (curr != nums[i]) {
curr = nums[i];
streak = 0;
}
while (i < nums.size() && nums[i] == curr) {
i++;
}
streak++;
curr++;
res = max(res, streak);
}
return res;
}
};
Time & Space Complexity
Time complexity: O(nlogn)
Space complexity: O(1) or O(n) depending on the sorting algorithm.
*/
/* Hash Set
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> numSet(nums.begin(), nums.end());
int longest = 0;
for (int num : numSet) {
if (numSet.find(num - 1) == numSet.end()) {
int length = 1;
while (numSet.find(num + length) != numSet.end()) {
length++;
}
longest = max(longest, length);
}
}
return longest;
}
};
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(n)
*/
/* Hash Map
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_map<int, int> mp;
int res = 0;
for (int num : nums) {
if (!mp[num]) {
mp[num] = mp[num - 1] + mp[num + 1] + 1;
mp[num - mp[num - 1]] = mp[num];
mp[num + mp[num + 1]] = mp[num];
res = max(res, mp[num]);
}
}
return res;
}
};
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(n)
*/