From 9c291ec8b7b2d36ff4f9ace832bd57378594e76f Mon Sep 17 00:00:00 2001 From: gayuna Date: Tue, 10 Dec 2024 19:20:32 +0900 Subject: [PATCH] add solutions for week 1 --- contains-duplicate/gayuna.py | 18 ++++++++++++++ house-robber/gayuna.py | 27 ++++++++++++++++++++ longest-consecutive-sequence/gayuna.py | 34 ++++++++++++++++++++++++++ top-k-frequent-elements/gayuna.py | 31 +++++++++++++++++++++++ valid-palindrome/gayuna.py | 20 +++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 contains-duplicate/gayuna.py create mode 100644 house-robber/gayuna.py create mode 100644 longest-consecutive-sequence/gayuna.py create mode 100644 top-k-frequent-elements/gayuna.py create mode 100644 valid-palindrome/gayuna.py diff --git a/contains-duplicate/gayuna.py b/contains-duplicate/gayuna.py new file mode 100644 index 000000000..e8a499ba0 --- /dev/null +++ b/contains-duplicate/gayuna.py @@ -0,0 +1,18 @@ +class Solution(object): + # Time complexity: O(n) + + # Iterate through all numbers. + # if there is same number in set, early return with True. + # if not exist, add the number to the set. + def containsDuplicate(self, nums): + """ + :type nums: List[int] + :rtype: bool + """ + my_set = set() + for i in nums: + if i in my_set: + return True + my_set.add(i) + + return False \ No newline at end of file diff --git a/house-robber/gayuna.py b/house-robber/gayuna.py new file mode 100644 index 000000000..c03c49786 --- /dev/null +++ b/house-robber/gayuna.py @@ -0,0 +1,27 @@ +class Solution(object): + """ + the robber will have 2 choices for each house + 1. include this house for target and skip next one + 2. bypass this house. + from house 1 to n, repeat this choice and use memoization for maximum money until the end to prevent duplicate calculation. + + time complexity: O(n) (forward iterating only) + """ + def rob(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + my_dict = {} + def process(idx): # -> maximum + if idx >= len(nums): + return 0 + + include_sum = nums[idx] + (my_dict[idx+2] if idx+2 in my_dict else process(idx+2)) + exclude_sum = my_dict[idx+1] if idx+1 in my_dict else process(idx+1) + my_dict[idx] = max(include_sum, exclude_sum) + return my_dict[idx] + + return process(0) + + \ No newline at end of file diff --git a/longest-consecutive-sequence/gayuna.py b/longest-consecutive-sequence/gayuna.py new file mode 100644 index 000000000..79f96d545 --- /dev/null +++ b/longest-consecutive-sequence/gayuna.py @@ -0,0 +1,34 @@ +class Solution(object): + # Time complexity: O(n) to generate set and O(n) to pop all items from set -> O(n) + O(n) = O(n) + + # create set from nums + # pop one number and calculate length with neighbors + # update longest_consivutive if the calculated length is bigger than existing number. + def longestConsecutive(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + start_dict = {} + + my_set = set(nums) + longest_consicutive = 0 + + while len(my_set): + num = my_set.pop() + length = 1 + start = num-1 + while start in my_set: + my_set.remove(start) + length += 1 + start -= 1 + end = num+1 + while end in my_set: + my_set.remove(end) + length += 1 + end += 1 + if length > longest_consicutive: + longest_consicutive = length + + return longest_consicutive + \ No newline at end of file diff --git a/top-k-frequent-elements/gayuna.py b/top-k-frequent-elements/gayuna.py new file mode 100644 index 000000000..94de9988e --- /dev/null +++ b/top-k-frequent-elements/gayuna.py @@ -0,0 +1,31 @@ +import heapq +class Solution(object): + # Time complexity: O(nlogn) + + # Iterate through all numbers, generate frequency dictionary. + # create max heap with frequency. + # pop k items from the heap + def topKFrequent(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: List[int] + """ + + freq_dict = {} + for i in nums: + if i in freq_dict: + freq_dict[i] += 1 + else: + freq_dict[i] = 1 + + my_heap = [] + for key, value in freq_dict.items(): + heapq.heappush(my_heap, (-value, key)) + + answer = [] + for i in range(k): + freq, number = heapq.heappop(my_heap) + answer.append(number) + + return answer \ No newline at end of file diff --git a/valid-palindrome/gayuna.py b/valid-palindrome/gayuna.py new file mode 100644 index 000000000..68dae0887 --- /dev/null +++ b/valid-palindrome/gayuna.py @@ -0,0 +1,20 @@ +class Solution(object): + # Time complexity: O(n) + + # Iterate through all characters, check for alphabet and number. trimmed string should only contain lower case letters and numbers. + # use two pointer. one from head and one from tail. iterate all char to the inside. + # early return if two characters are different. if loop ends, it's palindrome. + def isPalindrome(self, s): + """ + :type s: str + :rtype: bool + """ + trimmed = "" + for c in s: + if c.isalpha() or c.isnumeric(): + trimmed += c.lower() + + for i in range(len(trimmed)//2): + if trimmed[i] != trimmed[-(1+i)]: + return False + return True \ No newline at end of file