From 3879c7b8302f05d290986d0f614b323dbb54a43b Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 11 Dec 2024 22:10:07 -0500 Subject: [PATCH 01/15] contains-duplicate --- contains-duplicate/Jay-Mo-99.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 contains-duplicate/Jay-Mo-99.py diff --git a/contains-duplicate/Jay-Mo-99.py b/contains-duplicate/Jay-Mo-99.py new file mode 100644 index 000000000..0f2b02747 --- /dev/null +++ b/contains-duplicate/Jay-Mo-99.py @@ -0,0 +1,12 @@ +class Solution(object): + def containsDuplicate(self, nums): + """ + :type nums: List[int] + :rtype: bool + """ + #sets don't allow duplicate elements. + #If the length of the set created from nums is different from the original list(nums), It means there are duplicates. + if len(list(set(nums))) == len(nums): + return False + else: + return True \ No newline at end of file From c12452bc1a1ee77253eaccebb1eeabed14bbb02a Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 11 Dec 2024 22:33:34 -0500 Subject: [PATCH 02/15] contains-duplicate --- contains-duplicate/Jay-Mo-99.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/contains-duplicate/Jay-Mo-99.py b/contains-duplicate/Jay-Mo-99.py index 0f2b02747..d924eab7e 100644 --- a/contains-duplicate/Jay-Mo-99.py +++ b/contains-duplicate/Jay-Mo-99.py @@ -4,8 +4,19 @@ def containsDuplicate(self, nums): :type nums: List[int] :rtype: bool """ + #?? #sets don't allow duplicate elements. #If the length of the set created from nums is different from the original list(nums), It means there are duplicates. + + #Big O + #N: ??? ?? nums? ??(Length of the input list nums) + # + #Time Complexity: O(N) + #- set? nums? ?? n? ???? ????(Creating a set from nums): O(N) + #- ??? list? ?? nums?? ??? ??(Comparing the lengths between created list and original list) : O(1) + # + #Space Complexity: O(N) + #-set? nums? ??? ?? ????? n? ????(The set requires extra space depends on the size of nums) : O(N) if len(list(set(nums))) == len(nums): return False else: From 326d2901004ea116a259c7c28bc7d2c78d55ea0a Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 11 Dec 2024 22:34:30 -0500 Subject: [PATCH 03/15] contains-duplicate --- contains-duplicate/Jay-Mo-99.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/contains-duplicate/Jay-Mo-99.py b/contains-duplicate/Jay-Mo-99.py index d924eab7e..9b11678d7 100644 --- a/contains-duplicate/Jay-Mo-99.py +++ b/contains-duplicate/Jay-Mo-99.py @@ -1,22 +1,23 @@ -class Solution(object): +class Solution(object): def containsDuplicate(self, nums): """ :type nums: List[int] :rtype: bool """ - #?? - #sets don't allow duplicate elements. + #해석 + #sets는 복수 요소를 허용하지 않는다(sets don't allow duplicate elements.) + #만약 set에 기반된 list가 기존 nums와 길이가 다르다면 duplicate element가 있었다는 뜻이다. #If the length of the set created from nums is different from the original list(nums), It means there are duplicates. #Big O - #N: ??? ?? nums? ??(Length of the input list nums) - # + #N: 주어진 배열 nums의 길이(Length of the input list nums) + #Time Complexity: O(N) - #- set? nums? ?? n? ???? ????(Creating a set from nums): O(N) - #- ??? list? ?? nums?? ??? ??(Comparing the lengths between created list and original list) : O(1) - # + #- set은 nums의 길이 n에 기반하여 생성된다(Creating a set from nums): O(N) + #- 생성된 list와 기존 nums와의 비교는 상수(Comparing the lengths between created list and original list) : O(1) + #Space Complexity: O(N) - #-set? nums? ??? ?? ????? n? ????(The set requires extra space depends on the size of nums) : O(N) + #-set은 nums의 길이에 의해 생성되므로 n에 영향받음(The set requires extra space depends on the size of nums) : O(N) if len(list(set(nums))) == len(nums): return False else: From 85f70d11db9023297a46682b0cd4bf2270cca2f9 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 Dec 2024 01:09:03 -0500 Subject: [PATCH 04/15] solve: valid-palindrome --- valid-palindrome/Jay-Mo-99.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 valid-palindrome/Jay-Mo-99.py diff --git a/valid-palindrome/Jay-Mo-99.py b/valid-palindrome/Jay-Mo-99.py new file mode 100644 index 000000000..48fc8bca7 --- /dev/null +++ b/valid-palindrome/Jay-Mo-99.py @@ -0,0 +1,31 @@ +# --- 해석 --- +#매개변수 s를 소문자로 변환 후 non alpha numeric(알파벳과 숫자 이외의 모든 것)을 제거하고 빈칸을 replace로 제거한 후 temp에 저장한다 +#temp와 temp를 뒤집힌 string(temp[::-1])을 비교하여, 둘이 같으면 palindrome이다. + +# --- Big O +#N: 매개변수 s의 길이가 N이다. + +# Time Complexity: O(N) +#- temp는 s의 길이에 기반하여 생성된다: O(N) +#- 문자열 뒤집기(temp[::-1])도 s의 길이에 기반한다 : O(N) +#- temp == temp[::-1]는 두 문자열이 길이와 문자 하나하나가 같은지 확인 :O(N) + +# Space Complexity: O(N) +#-temp는 s의 길이에 의해 생성되므로 n에 영향받음(The temp requires extra space depends on the size of s) : O(N) + +class Solution(object): + def isPalindrome(self, s): + """ + :type s: str + :rtype: bool + """ + + #removes all non alpha numeric(only accept alphabet and number) items from the s + temp = lower(s) + temp = " ".join(re.split("[^a-zA-Z0-9]*",temp)).replace(" ","") + #Compare with temp and reverse temp + #If they are same, it is palindrome + if temp == temp[::-1]: + return True + else: + return False \ No newline at end of file From f82a7797f734c35e48bfd3491ca297dd71e41ae4 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 Dec 2024 01:36:25 -0500 Subject: [PATCH 05/15] Fix: including the Jay-Mo-99.py --- valid-palindrome/Jay-Mo-99.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/valid-palindrome/Jay-Mo-99.py b/valid-palindrome/Jay-Mo-99.py index 48fc8bca7..e17445d5a 100644 --- a/valid-palindrome/Jay-Mo-99.py +++ b/valid-palindrome/Jay-Mo-99.py @@ -28,4 +28,5 @@ def isPalindrome(self, s): if temp == temp[::-1]: return True else: - return False \ No newline at end of file + return False + \ No newline at end of file From 84c6595af29424ff1a0a214ce690944baf5e7e5d Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 Dec 2024 01:38:44 -0500 Subject: [PATCH 06/15] Fix: Add missing newline to Python files --- valid-palindrome/Jay-Mo-99.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/valid-palindrome/Jay-Mo-99.py b/valid-palindrome/Jay-Mo-99.py index e17445d5a..c8a516fc5 100644 --- a/valid-palindrome/Jay-Mo-99.py +++ b/valid-palindrome/Jay-Mo-99.py @@ -29,4 +29,10 @@ def isPalindrome(self, s): return True else: return False - \ No newline at end of file + + + + + + + From e35428fdad6e612cec127cc4badc6d325e60152d Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 Dec 2024 01:43:03 -0500 Subject: [PATCH 07/15] Fix: Add newline at end of Jay-Mo-99.py --- valid-palindrome/Jay-Mo-99.py | 1 + 1 file changed, 1 insertion(+) diff --git a/valid-palindrome/Jay-Mo-99.py b/valid-palindrome/Jay-Mo-99.py index c8a516fc5..5135cd51d 100644 --- a/valid-palindrome/Jay-Mo-99.py +++ b/valid-palindrome/Jay-Mo-99.py @@ -36,3 +36,4 @@ def isPalindrome(self, s): + From c3bfa2ccb275b70a1f8297ac8a876470edf830d8 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 Dec 2024 01:45:34 -0500 Subject: [PATCH 08/15] Fix: Add newline at end of contains-duplicate/Jay-Mo-99.py --- contains-duplicate/Jay-Mo-99.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contains-duplicate/Jay-Mo-99.py b/contains-duplicate/Jay-Mo-99.py index 9b11678d7..3e856980c 100644 --- a/contains-duplicate/Jay-Mo-99.py +++ b/contains-duplicate/Jay-Mo-99.py @@ -21,4 +21,4 @@ def containsDuplicate(self, nums): if len(list(set(nums))) == len(nums): return False else: - return True \ No newline at end of file + return True From 60c7a9105327fdbcfc0f146ac6b3f7c969bbd9e1 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 13 Dec 2024 01:32:06 -0500 Subject: [PATCH 09/15] Modified: contains-duplicate/Jay-Mo-99.py --- contains-duplicate/Jay-Mo-99.py | 1 + 1 file changed, 1 insertion(+) diff --git a/contains-duplicate/Jay-Mo-99.py b/contains-duplicate/Jay-Mo-99.py index 3e856980c..584f92469 100644 --- a/contains-duplicate/Jay-Mo-99.py +++ b/contains-duplicate/Jay-Mo-99.py @@ -22,3 +22,4 @@ def containsDuplicate(self, nums): return False else: return True + \ No newline at end of file From 58988dd5efad7d576a5c514894909e5fb1537e68 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 13 Dec 2024 01:35:32 -0500 Subject: [PATCH 10/15] Fix: Add missing newline at EOF --- contains-duplicate/Jay-Mo-99.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contains-duplicate/Jay-Mo-99.py b/contains-duplicate/Jay-Mo-99.py index 584f92469..d33bfde1d 100644 --- a/contains-duplicate/Jay-Mo-99.py +++ b/contains-duplicate/Jay-Mo-99.py @@ -22,4 +22,5 @@ def containsDuplicate(self, nums): return False else: return True - \ No newline at end of file + + From 9877f4270a843cae2737976bd5289df44e7ecbf8 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 13 Dec 2024 01:52:41 -0500 Subject: [PATCH 11/15] Edit: Delete if statement and return condition directely --- contains-duplicate/Jay-Mo-99.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/contains-duplicate/Jay-Mo-99.py b/contains-duplicate/Jay-Mo-99.py index d33bfde1d..25a81a0c0 100644 --- a/contains-duplicate/Jay-Mo-99.py +++ b/contains-duplicate/Jay-Mo-99.py @@ -18,9 +18,6 @@ def containsDuplicate(self, nums): #Space Complexity: O(N) #-set은 nums의 길이에 의해 생성되므로 n에 영향받음(The set requires extra space depends on the size of nums) : O(N) - if len(list(set(nums))) == len(nums): - return False - else: - return True + return len(list(set(nums))) != len(nums) #set된 list와 기존 nums의 len이 일치하지 않는다면 true(duplicate), 아니면 false From 269e6ab25e951a2e3d209d1baf07be8105eaa26e Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 13 Dec 2024 01:55:39 -0500 Subject: [PATCH 12/15] Edit: Delete if statement and return condition directely --- valid-palindrome/Jay-Mo-99.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/valid-palindrome/Jay-Mo-99.py b/valid-palindrome/Jay-Mo-99.py index 5135cd51d..a1753d2f2 100644 --- a/valid-palindrome/Jay-Mo-99.py +++ b/valid-palindrome/Jay-Mo-99.py @@ -25,10 +25,9 @@ def isPalindrome(self, s): temp = " ".join(re.split("[^a-zA-Z0-9]*",temp)).replace(" ","") #Compare with temp and reverse temp #If they are same, it is palindrome - if temp == temp[::-1]: - return True - else: - return False + return temp == temp[::-1] + + From dc6b6ad49c0b603802b5a6dea855f10469cc0538 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 13 Dec 2024 17:18:44 -0500 Subject: [PATCH 13/15] Solve: topKFrequent --- top-k-frequent-elements/Jay-Mo-99.py | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 top-k-frequent-elements/Jay-Mo-99.py diff --git a/top-k-frequent-elements/Jay-Mo-99.py b/top-k-frequent-elements/Jay-Mo-99.py new file mode 100644 index 000000000..87434cb26 --- /dev/null +++ b/top-k-frequent-elements/Jay-Mo-99.py @@ -0,0 +1,43 @@ + #해석 + #nums를 dictionary로 전환한다. nums의 기존 element가 key, element의 갯수를 value값으로 지정한다. + #value값이 큰 순서대로 dictionary의 요소를 정렬한다 + #그중 0에서 k-1까지의 key값만을 반환한다. + + #Big O + #N: 주어진 list의 길이 nums (Length of the input list nums) + #M: count 딕셔너리의 고유한 key 의 갯수 + + #Time Complexity: O(NLogN) + #- count: N의 길이에 기반하여 생성된다 : O(N) + #- sorted_count: 파이썬의 sorted 메소드는 Timesort 알고리즘 기반의 비교 기반 정렬 알고리즘 + # 리스트의 길이가 M이면 sorted()는 요소들을 쪼개고 병합하여 정렬한다. 대략 M개의 요소를 logM번 비교 + + #Space Complexity: O(N) + #- count: M개에 기반하여 Dictionary 공간 저장: O(M) + #- sorted_count: count.items의 갯수에 기반하여 공간 저장 : O(M) + #- dic_count: 상위 요소 갯수(k개) 만큼 저장 : O(k) + #- 최종: O(N) + O(M) + O(k): k는 최대 M개 만큼 가능하고 M은 최대 N개만큼 가능하다. + +class Solution(object): + def topKFrequent(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: List[int] + """ + + #Create dictionary which have a key(element of nums) and value(count num of the element) + count = {} + for i in nums: + try: count[i] += 1 + except: count[i] = 1 + + #Sort depends on the value descending order + sorted_count = sorted(count.items(),key=lambda x:x[1],reverse=True) + #k까지의 요소만을 dictionary data type으로 convert + dic_count = dict(sorted_count[:k]) + #Return keys + return dic_count.keys() + + + From a560bdd43ddd75cd78d60350b2ad31118b25769d Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 13 Dec 2024 20:46:19 -0500 Subject: [PATCH 14/15] Solve: Longest Consecutive Sequence --- longest-consecutive-sequence/Jay-Mo-99.py | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 longest-consecutive-sequence/Jay-Mo-99.py diff --git a/longest-consecutive-sequence/Jay-Mo-99.py b/longest-consecutive-sequence/Jay-Mo-99.py new file mode 100644 index 000000000..bba6db868 --- /dev/null +++ b/longest-consecutive-sequence/Jay-Mo-99.py @@ -0,0 +1,49 @@ +# --- 해석 --- +#매개변수 nums 를 set으로 바꿔서 중복제거 +#set을 list로 전환하여 sort하여 오름차순으로 변환 +#for loop 로 nums[1]부터 nums[len(nums)-1] 에 접근 +#nums[i]-nums[i-1] =1 이면 변수 current를 증가, longest는 current의 최댓값을 업데이트 +#만약 해당 조건을 만족하지 않는다면 current를 1로 초기화, 이후 longest 값 return + +# --- Big O +#N: 매개변수 nums의 길이가 N이다. + +# Time Complexity: O(N) +#- set(nums)는 nums의모든 요소를 하나씩 순회하며 중복 제거 : O(N) +#- list(set(nums)) 는 data type을 변환하면서 nums의 갯수만큼 data를 추가 생성: O(N) +#- for loop 는 nums[1] 부터 nums[len(nums)-1]만큼 순회: O(N) + +# Space Complexity: O(N) +#-nums = list(set(nums)): list(),set(),dict() 이런 data type을 변환할때마다 N만큼 공간 추가할당: O(N) +#-longest,current 변수는 상수 : O(1) + +class Solution(object): + def longestConsecutive(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + #If the nums doesn't have elements, return 0 + if len(nums) ==0: + return 0 + #중복제거 set + nums = list(set(nums)) + #sort + nums.sort() + print(nums) + + #Variables + longest = 1 + current = 1 + + #Approah all element of nums for checking the sequnece number or not + for i in range(1,len(nums)): + if nums[i] == nums[i-1] + 1: + current +=1 #current는 nums[i]와 nums[i-1]의 차이가 1이면 +1해줌. + longest = max(longest, current) #return값을 위해 currrent가 가장 크도록 업데이트 + else:#연속 되지 않을 시 current 1로 초기화 + current =1 + print(longest) + return longest + + From 3dc6420e3d2758b3d823cd18afffdaf0f50a61f3 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 13 Dec 2024 21:16:06 -0500 Subject: [PATCH 15/15] Solve: House Robber --- house-robber/Jay-Mo-99.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 house-robber/Jay-Mo-99.py diff --git a/house-robber/Jay-Mo-99.py b/house-robber/Jay-Mo-99.py new file mode 100644 index 000000000..4d4021e23 --- /dev/null +++ b/house-robber/Jay-Mo-99.py @@ -0,0 +1,37 @@ +# --- 해석 --- +#매개변수 nums 를 순회하면서 nums[i] 일때의 최대 누적값을 업데이트 한다 +#prev1은 현재까지의 최고 누적금액, prev2는 이전 집까지의 최고 누적금액이다. +#현재 집 nums[i] 를 도둑질 하려면, 이전 집까지의 최고금액(prev2) + 현재 집(nums[i])이다. +#현재 집 nums[i]를 도둑질에 제외하려면 현재까지의 최고 금엑(prev1) 이다. +#loop 당 저 둘의 최댓값을 선택하여 current변수에 update해준다. + +# --- Big O +#N: 매개변수 nums의 길이가 N이다. + +# Time Complexity: O(N) +#- for loop 는 nums[0] 부터 nums[len(nums)]만큼 순회: O(N) + +# Space Complexity: O(1) +#-current,prev1,prev2 는 nums와 무관한 상수 메로리 할당: O(1) + + +class Solution(object): + def rob(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + #prev1: 현재 집까지의 최고 금액 + #prev2: 이이전 집까지의 최고 금액 + prev1,prev2=0,0 + + for num in nums: + #current는 prev1과 prev2+num 중 큰 값을 update + current = max(prev1,prev2+num) + prev2 = prev1 #current업데이트 이후 prev1(현재 최고금액) 이 prev2(이어진 집까지 최고금액)가된다 + prev1= current #prev1은 현 num까지 고려된 current의 값이다. (현재 최고 금액액) + return prev1 +nums = [2,7,9,3,1] +solution = Solution() +solution.rob(nums) +