Skip to content

Commit cfb249f

Browse files
author
Albert Hu
authored
Merge pull request #25 from alberthu16/day36
Day 36: Longest consecutive sequence (sorting?)
2 parents 664954e + 00abd4d commit cfb249f

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

day36/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Question of the day: https://leetcode.com/problems/longest-consecutive-sequence/#/description
2+
3+
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
4+
5+
For example,
6+
Given `[100, 4, 200, 1, 3, 2]`,
7+
The longest consecutive elements sequence is `[1, 2, 3, 4]`. Return its
8+
length: `4`.
9+
10+
Your algorithm should run in `O(n)` complexity.
11+
12+
## Ideas
13+
14+
Clarification questions: can I make any assumptions about the integers in the
15+
input array? I.e. is there a range like 0 - 100? Are all integers in the input
16+
array unique or can there be duplicates? Do duplicates count in consecutive
17+
sequences?
18+
19+
Radix sort allows me to do a tradeoff on space for faster time complexity. The
20+
radix I can use here instead of digit-by-digit, is to treat the max valued element
21+
of the array as the limit of a single, theoretical digit. That way, in one
22+
additional pass through, I will have the input sorted. In a third pass through,
23+
I can count the longest consecutive sequence.
24+
25+
This approach is fine for input arrays with elements that aren't too large.
26+
However, the runtime is actually proportional to the value of the largest element.
27+
Both the runtime and space complexity are `O(k)` where `k` is the value of
28+
the largest element.
29+
30+
Is there a better approach? Not sure. Let's code up the first solution and
31+
see if I can think of anything by the end.
32+
33+
## Code
34+
35+
[Python](./longestConsecutive.py)
36+
37+
## Follow up
38+
39+
Welp.. didn't actually solve the problem under the right constraints. How can I
40+
be more efficient?

day36/longestConsecutive.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def longestConsecutive(nums):
2+
if len(nums) <= 1:
3+
return len(nums)
4+
5+
buckets = [False for i in xrange(max(nums)+1)]
6+
for num in nums:
7+
buckets[num] = True
8+
if len(buckets) == 1:
9+
return 1
10+
11+
maxLongestCount, longestCount = 0, 1
12+
for prev, curr in zip(buckets, buckets[1:]):
13+
if prev and curr:
14+
longestCount += 1
15+
else:
16+
longestCount = 1
17+
maxLongestCount = max(maxLongestCount, longestCount)
18+
19+
return maxLongestCount
20+
21+
def testLongestConsecutive():
22+
assert longestConsecutive([100, 4, 200, 1, 3, 2]) == 4
23+
assert longestConsecutive([]) == 0
24+
assert longestConsecutive([0]) == 1
25+
assert longestConsecutive([0, 0]) == 1 # assume duplicates don't count
26+
assert longestConsecutive([0, 1, 2]) == 3
27+
assert longestConsecutive([2, 1, 0]) == 3
28+
assert longestConsecutive([1, 50, 2, 51, 3, 52, 4, 53, 54]) == 5
29+
30+
def main():
31+
testLongestConsecutive()
32+
33+
if __name__ == "__main__":
34+
main()

0 commit comments

Comments
 (0)