forked from SandeepanM2003/cpp_python_codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding some instructions, contributors page & 99 DailyCodingChalanges…
… solution
- Loading branch information
1 parent
0fe85ed
commit cf8351d
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,3 @@ | ||
# Contributors | ||
|
||
- [FelipeCRamos](github.com/felipecramos) |
This file contains 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,39 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Problem: | ||
Given an unsorted array of integers, find the length of the longest consecutive | ||
elements sequence. | ||
For example, given [100, 4, 200, 1, 3, 2], the longest consecutive element sequence is | ||
[1, 2, 3, 4]. Return its length: 4. | ||
Your algorithm should run in O(n) complexity. | ||
Solution: | ||
Time complexity: O(n) | ||
Space complexity: O(n) | ||
""" | ||
|
||
from typing import List | ||
|
||
# lces = Longest consecutive elements sequence | ||
def lces(arr: List[int]) -> int: | ||
unique_elements = set(arr) | ||
longest_sequence = 0 | ||
# generating the longest sequence length | ||
for i in range(len(arr)): | ||
if (arr[i] - 1) not in unique_elements: | ||
# current element is the starting element of a sequence | ||
j = arr[i] | ||
while j in unique_elements: | ||
j += 1 | ||
# update longest sequence length | ||
longest_sequence = max(longest_sequence, j - arr[i]) | ||
return longest_sequence | ||
|
||
|
||
if __name__ == "__main__": | ||
# test input/outputs | ||
print(lces([100, 4, 200, 1])) | ||
print(lces([100, 4, 200, 1, 3])) | ||
print(lces([100, 4, 200, 2, 3])) | ||
print(lces([100, 4, 200, 1, 3, 2])) | ||
print(lces([100, 4, 200, 1, 3, 2, 5])) |
This file contains 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,4 @@ | ||
# Hello contributors | ||
|
||
This folder is where your python solutions should be placed! | ||
Please give at least one input/output example for the problem! (can be inside of comments) |