-
-
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
Open
Sarthak950
wants to merge
20
commits into
TheAlgorithms:master
Choose a base branch
from
Sarthak950:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all 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 66aa0a6
change the formatting in the explanation
Sarthak950 2e406a0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dcf7fa0
Update data_structures/arrays/longest_consecutive_sequence.py
Sarthak950 0ec6a75
Merge branch 'TheAlgorithms:master' into master
Sarthak950 5baa500
removed the function from the class and fixed the indentation #9686
Sarthak950 7484018
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0a5eb1b
converted function name from CamelCase to snake_case
Sarthak950 92c70f5
added the docktest for the solution
Sarthak950 4db5841
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ca07301
removed an accidental error
Sarthak950 708966e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d10a45f
Solved some linter errors
Sarthak950 9d2e88e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 56fbcbe
added the return type
Sarthak950 37ec51a
argument hint
Sarthak950 ca3192f
List - list
Sarthak950 fae996c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] afaa185
fixed the indentation in the doctests string
Sarthak950 f5fa312
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,57 @@ | ||||||||
""" | ||||||||
author: Sarthak Sharma https://github.com/Sarthak950 | ||||||||
Website: 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 longest_consecutive_sequence(nums: list[int]) -> int: | ||||||||
""" | ||||||||
Finds the length of the longest consecutive sequence in a list of numbers. | ||||||||
|
||||||||
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 | ||||||||
""" | ||||||||
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]. | ||||||||
""" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.