diff --git a/neetcode/arrays/encode_decode_strings/README.md b/neetcode/arrays/encode_decode_strings/README.md index e69de29..8b13789 100644 --- a/neetcode/arrays/encode_decode_strings/README.md +++ b/neetcode/arrays/encode_decode_strings/README.md @@ -0,0 +1 @@ + diff --git a/neetcode/arrays/longest_consecutive_sequence/README.md b/neetcode/arrays/longest_consecutive_sequence/README.md new file mode 100644 index 0000000..0a93231 --- /dev/null +++ b/neetcode/arrays/longest_consecutive_sequence/README.md @@ -0,0 +1,55 @@ +Longest Consecutive Sequence +Given an array of integers nums, return the length of the longest consecutive sequence of elements that can be formed. + +A consecutive sequence is a sequence of elements in which each element is exactly 1 greater than the previous element. The elements do not have to be consecutive in the original array. + +You must write an algorithm that runs in O(n) time. + +Example 1: + +Input: nums = [2,20,4,10,3,4,5] + +Output: 4 +Explanation: The longest consecutive sequence is [2, 3, 4, 5]. + +Example 2: + +Input: nums = [0,3,2,5,4,6,1,1] + +Output: 7 +Constraints: + +0 <= nums.length <= 1000 +-10^9 <= nums[i] <= 10^9 + +# Solution + +[2,20,4,10,3,4,5] + +# 1 +e = 2 +[[2]] + +# 2 +e = 20 +[[2], [20]] + +# 3 +e = 4 +[[2], [20], [4]] + +# 4 +e = 10 +[[2], [20], [4], [10]] + +# 5 +e = 3 +[[2,3], [20], [4], [10]] + +# 6 +e = 4 +[[2,3,4], [20], [4], [10]] + +# 7 +e = 5 +[[2,3,4,5], [20], [4], [10]] diff --git a/neetcode/arrays/longest_consecutive_sequence/solution.py b/neetcode/arrays/longest_consecutive_sequence/solution.py new file mode 100644 index 0000000..9918bab --- /dev/null +++ b/neetcode/arrays/longest_consecutive_sequence/solution.py @@ -0,0 +1,33 @@ +from typing import List +import pytest + + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + if len(nums) == 0: + return 0 + res: List[List[int]] = [] + for i in range(0, len(nums)): + if i == 0: + res.append([nums[i]]) + continue + e = nums[i] + for ri in range(0, len(res)): + if res[ri][-1] + 1 == e: + res[ri].append(e) + break + res.append([e]) + return res + return max([len(r) for r in res]) + + +@pytest.mark.parametrize( + "input_value, expected_value", + [ + ([2, 20, 4, 10, 3, 4, 5], 4), + ([0, 3, 2, 5, 4, 6, 1, 1], 7), + ], +) +def test_solution(input_value, expected_value): + s = Solution() + assert s.longestConsecutive(nums=input_value) == expected_value diff --git a/neetcode/linked_list/reverse_linked_list/README.md b/neetcode/linked_list/reverse_linked_list/README.md new file mode 100644 index 0000000..2837012 --- /dev/null +++ b/neetcode/linked_list/reverse_linked_list/README.md @@ -0,0 +1,68 @@ +# Task +Reverse Linked List +Given the beginning of a singly linked list head, reverse the list, and return the new beginning of the list. + +Example 1: + +Input: head = [0,1,2,3] + +Output: [3,2,1,0] +Example 2: + +Input: head = [] + +Output: [] +Constraints: + +0 <= The length of the list <= 1000. +-1000 <= Node.val <= 1000 + +# Solution +## 0 +ll = [0, 1, 2, 3] +p = None +c = 0 +n = 1 + +## 1 + p + c + n +[0, 1, 2, 3] + +``` +if p == c + p.next = None + c = n + n = c.next +``` + +## 2 + p + c + n +[0, 1, 2, 3] +``` +c.next = p +p = c +c = n +n = c.next +``` + +## 3 + p + c + n +[0, 1, 2, 3] +``` +c.next = p +p = c +c = n +n = c.next +``` +## 4 + p + c + n = None +[0, 1, 2, 3] + diff --git a/neetcode/linked_list/reverse_linked_list/main.py b/neetcode/linked_list/reverse_linked_list/main.py new file mode 100644 index 0000000..0d9d8bd --- /dev/null +++ b/neetcode/linked_list/reverse_linked_list/main.py @@ -0,0 +1,46 @@ +from typing import Optional + + +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + if not head: + return None + if head.next is None: + return head + p = head + c = head + n = head.next + while True: + if p == c: + p.next = None + c = n + n = c.next + if c.next is None: + c.next = p + break + else: + c.next = p + p = c + c = n + n = c.next + return c + + +if __name__ == "__main__": + s = Solution() + e3 = ListNode(3, None) + e2 = ListNode(2, e3) + e1 = ListNode(1, e2) + e0 = ListNode(0, e1) + k = [e0, e1, e2, e3] + nb = s.reverseList(e0) + for index, elem in enumerate(k): + next_val = elem.next.val if elem.next else "No value" + print(f"{index} - v: {elem.val} - vn: {next_val}")