From 10cd65a0ca7521a54e8bf33453dbd8a6573acfaa Mon Sep 17 00:00:00 2001 From: helenapark0826 Date: Fri, 13 Dec 2024 01:51:09 -0500 Subject: [PATCH] Week 1 Solutions: containsDuplicate, validPalindrome, topKFrequentElements and longestConsecutiveSequence --- contains-duplicate/yolophg.py | 18 ++++++++++++++++++ longest-consecutive-sequence/yolophg.py | 25 +++++++++++++++++++++++++ top-k-frequent-elements/yolophg.py | 16 ++++++++++++++++ valid-palindrome/yolophg.py | 12 ++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 contains-duplicate/yolophg.py create mode 100644 longest-consecutive-sequence/yolophg.py create mode 100644 top-k-frequent-elements/yolophg.py create mode 100644 valid-palindrome/yolophg.py diff --git a/contains-duplicate/yolophg.py b/contains-duplicate/yolophg.py new file mode 100644 index 000000000..bd50d5073 --- /dev/null +++ b/contains-duplicate/yolophg.py @@ -0,0 +1,18 @@ +# Time Complexity: O(n) +# Space Complexity: O(n) + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + # set to keep track of duplicates. + duplicates = set() + + # go through each number in the list + for num in nums: + # if it's a duplicate, return true. + if num in duplicates: + return True + # otherwise, add it to the set to check for duplicates. + duplicates.add(num) + + # if finish the loop and don't find duplicates, return false. + return False diff --git a/longest-consecutive-sequence/yolophg.py b/longest-consecutive-sequence/yolophg.py new file mode 100644 index 000000000..09dcbe78d --- /dev/null +++ b/longest-consecutive-sequence/yolophg.py @@ -0,0 +1,25 @@ +# Time Complexity: O(n) +# Space Complexity: O(n) + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + # convert list to set to remove duplicates and allow quick lookups + num_set = set(nums) + longest_streak = 0 + + # loop through each number in the set + for num in num_set: + # only start counting if it's the beginning of a sequence + if num - 1 not in num_set: + current_num = num + current_streak = 1 + + # keep counting while the next number in the sequence exists + while current_num + 1 in num_set: + current_num += 1 + current_streak += 1 + + # update the longest streak found so far + longest_streak = max(longest_streak, current_streak) + + return longest_streak diff --git a/top-k-frequent-elements/yolophg.py b/top-k-frequent-elements/yolophg.py new file mode 100644 index 000000000..1c4272116 --- /dev/null +++ b/top-k-frequent-elements/yolophg.py @@ -0,0 +1,16 @@ +# Time Complexity: O(n log n) +# Space Complexity: O(n) + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + # count how many times each number appears + counts = {} + for num in nums: + counts[num] = counts.get(num, 0) + 1 + + # sort numbers by their count (most frequent first) and grab top k + # counts.get gets the count of num + # reverse=True sorts in descending order + # [:k] gets the first k elements + top_nums = sorted(counts, key=counts.get, reverse=True) + return top_nums[:k] diff --git a/valid-palindrome/yolophg.py b/valid-palindrome/yolophg.py new file mode 100644 index 000000000..cd353634c --- /dev/null +++ b/valid-palindrome/yolophg.py @@ -0,0 +1,12 @@ +# Time Complexity: O(n) +# Space Complexity: O(n) + +class Solution: + def isPalindrome(self, s: str) -> bool: + # clean up the string: remove non-alphanumeric chars and convert to lowercase + # isalnum() checks if the character is alphanumeric + filtered = ''.join(filter(str.isalnum, s)).lower() + + # check if it reads the same forwards and backwards + # filtered[::-1] flips the string + return filtered == filtered[::-1]