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 18 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
63 changes: 63 additions & 0 deletions data_structures/arrays/longest_consecutive_sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
author: Sarthak Sharma https://github.com/Sarthak950
Website: 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 longest_consecutive_sequence(nums: list[int]) -> int:
"""
Finds the length of the longest consecutive sequence in a list of numbers.

Args:
nums (List[int]): A list of integers.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please refrain from declaring types within a docstring.


Returns:
int: The length of the longest consecutive sequence.

Examples:
>>> longest_consecutive_sequence([100, 4, 200, 1, 3, 2])
4
>>> longest_consecutive_sequence([1, 2, 3, 4, 5])
5
>>> longest_consecutive_sequence([5, 4, 3, 2, 1])
5
>>> longest_consecutive_sequence([])
0
"""
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
"""
Finds the length of the longest consecutive sequence in a list of numbers.
Args:
nums (List[int]): A list of integers.
Returns:
int: The length of the longest consecutive sequence.
Examples:
>>> longest_consecutive_sequence([100, 4, 200, 1, 3, 2])
4
>>> longest_consecutive_sequence([1, 2, 3, 4, 5])
5
>>> longest_consecutive_sequence([5, 4, 3, 2, 1])
5
>>> longest_consecutive_sequence([])
0
"""
"""
Finds the length of the longest consecutive sequence in a list of numbers.
nums: A list of integers.
int: The length of the longest consecutive sequence.
>>> longest_consecutive_sequence([100, 4, 200, 1, 3, 2])
4
>>> longest_consecutive_sequence([1, 2, 3, 4, 5])
5
>>> longest_consecutive_sequence([5, 4, 3, 2, 1])
5
>>> longest_consecutive_sequence([])
0
"""

Please read CONTRIBUTING.md to see how to indent docstrings.

if not nums:
return 0

# Create a set of all the numbers in the list
num_set = set(nums)
longest_sequence = 0

# Iterate over the set of numbers
for num in num_set:
# If the number that is one less than the current number is not in the set,
# then this is the beginning of a sequence
if num - 1 not in num_set: # Start of a potential sequence
# Store the current number in a variable
# and initiate a sequence length counter
current_num = num
current_sequence = 1

# While the next number is in the set,
# increment the current number and the sequence counter
while current_num + 1 in num_set:
current_num += 1
current_sequence += 1

# Update the longest sequence if the current sequence is longer
longest_sequence = max(longest_sequence, current_sequence)

return longest_sequence


"""
This code takes in a list of numbers and returns the length of
the longest sequence of consecutive numbers in the list.
For example, if the list is [1, 3, 2, 4, 5, 6, 7], the function will return 5,
since the longest sequence of consecutive numbers is [3, 4, 5, 6, 7].
"""