Longest Consecutive Sequence – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Given an unsorted array of integers nums
, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n)
time.
Example 1:
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]
. Therefore its length is 4.
Example 2:
Input: nums = [0,3,7,2,5,8,4,6,0,1] Output: 9
Constraints:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
C++ Longest Consecutive Sequence LeetCode Solution
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if (nums.size() == 0) return 0;
sort(nums.begin(), nums.end());
int maxLen = 0, currLen = 1;
for (int i = 1; i < nums.size(); i++) {
if (nums[i] - 1 == nums[i - 1]) {
currLen += 1;
}
else if (nums[i] != nums[i - 1]){
maxLen = max(maxLen, currLen);
currLen = 1;
}
}
maxLen = max(currLen, maxLen);
return maxLen;
}
};
Java Longest Consecutive Sequence LeetCode Solution
public int longestConsecutive(int[] arr) {
int n=arr.length;
if(n==0){
return 0;
}
HashSet<Integer> hs=new HashSet<>();
for(Integer e : arr) {
hs.add(e);
}
int maxCt=1;
for(Integer e: arr){
if(hs.contains(e)){
int ct=1;
hs.remove(e);
int prev=e-1;
while(hs.contains(prev)){
hs.remove(prev);
ct++;
prev--;
}
int next=e+1;
while(hs.contains(next)){
hs.remove(next);
ct++;
next++;
}
maxCt=Math.max(maxCt,ct);
}
}
return maxCt;
}
Python 3 Longest Consecutive Sequence LeetCode Solution
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
hash_set = set(nums)
streak = 1
highest_streak = 0
while len(hash_set) > 0:
streak = 1
num = hash_set.pop()
num_add = num
while num_add + 1 in hash_set:
streak += 1
num_add += 1
hash_set.remove(num_add)
num_minus = num
while num_minus - 1 in hash_set:
streak += 1
num_minus -= 1
hash_set.remove(num_minus)
if highest_streak < streak:
highest_streak = streak
return highest_streak
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1
Leave a comment below