Skip to content
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

Longest Consecutive Sequence LeetCode problem #9686

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
cc60085
Longest Consecutive Sequence Leet code Prooblem in python
Sarthak950 Oct 4, 2023
66aa0a6
change the formatting in the explanation
Sarthak950 Oct 4, 2023
2e406a0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
dcf7fa0
Update data_structures/arrays/longest_consecutive_sequence.py
Sarthak950 Oct 5, 2023
0ec6a75
Merge branch 'TheAlgorithms:master' into master
Sarthak950 Oct 5, 2023
5baa500
removed the function from the class and fixed the indentation #9686
Sarthak950 Oct 5, 2023
7484018
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
0a5eb1b
converted function name from CamelCase to snake_case
Sarthak950 Oct 5, 2023
92c70f5
added the docktest for the solution
Sarthak950 Oct 5, 2023
4db5841
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
ca07301
removed an accidental error
Sarthak950 Oct 5, 2023
708966e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
d10a45f
Solved some linter errors
Sarthak950 Oct 5, 2023
9d2e88e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
56fbcbe
added the return type
Sarthak950 Oct 5, 2023
37ec51a
argument hint
Sarthak950 Oct 5, 2023
ca3192f
List - list
Sarthak950 Oct 5, 2023
fae996c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
afaa185
fixed the indentation in the doctests string
Sarthak950 Oct 5, 2023
f5fa312
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions data_structures/arrays/longest_consecutive_sequence.py
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

"""

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from collections import defaultdict


def longestConsecutiveSequence(self, nums: List[int]) -> int:
# Create a map of all numbers in the array to their index

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: data_structures/arrays/longest_consecutive_sequence.py

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
    ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: data_structures/arrays/longest_consecutive_sequence.py

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.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

"""