-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Longest Consecutive Sequence LeetCode problem #9686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
cc60085
66aa0a6
2e406a0
dcf7fa0
0ec6a75
5baa500
7484018
0a5eb1b
92c70f5
4db5841
ca07301
708966e
d10a45f
9d2e88e
56fbcbe
37ec51a
ca3192f
fae996c
afaa185
f5fa312
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,64 @@ | ||||||||
""" | ||||||||
author: Sarthak Sharma https://github.com/Sarthak950 https://sarthak950.netlify.app | ||||||||
date: 4 OCT 2023 | ||||||||
Longest Consecutive Sequence Problem from LeetCode | ||||||||
|
||||||||
""" | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
def longestConsecutiveSequence(self, nums: List[int]) -> int: | ||||||||
# Create a map of all numbers in the array to their index | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 10:5.
parser error: error at 9:4: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
# Create a map of all numbers in the array to their index
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 10:5.
parser error: error at 9:4: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
# Create a map of all numbers in the array to their index
^ |
||||||||
nmap = defaultdict(int) | ||||||||
for i in range(len(nums)): | ||||||||
if nums[i] not in nmap: | ||||||||
nmap[nums[i]] = i | ||||||||
|
||||||||
# Create a seen array to keep track of whether a number has been seen before | ||||||||
seen, ans = [0] * len(nums), 0 | ||||||||
|
||||||||
# Iterate through each number in the array | ||||||||
for n in nums: | ||||||||
# Set curr to the current number, and count to 1 | ||||||||
curr, count = n, 1 | ||||||||
|
||||||||
# If we've already seen this number, skip it | ||||||||
if seen[nmap[n]]: | ||||||||
continue | ||||||||
|
||||||||
# Otherwise, iterate through all consecutive numbers after curr | ||||||||
while curr + 1 in nmap: | ||||||||
# Increment curr | ||||||||
curr += 1 | ||||||||
|
||||||||
# Check if we've seen this number before | ||||||||
ix = nmap[curr] | ||||||||
if seen[ix]: | ||||||||
# If we have, add it to the count and break out of the loop | ||||||||
count += seen[ix] | ||||||||
break | ||||||||
else: | ||||||||
# Otherwise, add it to the seen array and increment the count | ||||||||
seen[ix] = 1 | ||||||||
count += 1 | ||||||||
|
||||||||
# Add the count to the seen array and update the answer | ||||||||
seen[nmap[n]], ans = count, max(ans, count) | ||||||||
|
||||||||
# Return the answer | ||||||||
return ans | ||||||||
|
||||||||
|
||||||||
""" | ||||||||
Idea | ||||||||
1. First, we put all the numbers into a dictionary, and note the index of each number. | ||||||||
This is to make sure the lookup time when we check if a number is in the list is O(1). | ||||||||
We also initialize a seen list for later use. | ||||||||
|
||||||||
2. Then, we loop through the numbers, and for each number n, we check if it's already in seen list. | ||||||||
If so, we skip it. Otherwise, we start counting the length of the consecutive sequence starting from n. | ||||||||
This is done by checking if n+1 is in the dictionary. If so, we increment the counter, and set the seen status of n to 1. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make sure all lines are less than 88 characters long |
||||||||
Otherwise, we set the seen status of n to 1, and break the loop. | ||||||||
This is because if n+1 is not in the dictionary, then the consecutive sequence starting from n is over. | ||||||||
And we don't need to count the length of the consecutive sequence starting from n+1, since we will eventually count it when we reach n+1. | ||||||||
|
||||||||
""" |
Uh oh!
There was an error while loading. Please reload this page.