From c17a4b3b85ef0f92939cdf5c61f3f71238ae817c Mon Sep 17 00:00:00 2001 From: mand2 Date: Fri, 3 May 2024 00:57:26 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[mand2]=20week1=20=EB=8B=B5=EC=95=88=20?= =?UTF-8?q?=EC=A0=9C=EC=B6=9C=20-=20[x]=20Contains=20Duplicate=20-=20[x]?= =?UTF-8?q?=20Valid=20Anagram=20-=20[x]=20Two=20Sum=20-=20[x]=20Valid=20Pa?= =?UTF-8?q?lindrome=20-=20[x]=20Best=20Time=20to=20Buy=20and=20Sell=20Stoc?= =?UTF-8?q?k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +++ best-time-to-buy-and-sell-stock/mand2.py | 20 ++++++++++++++ contains-duplicate/mand2.py | 16 ++++++++++++ two-sum/mand2.py | 25 ++++++++++++++++++ valid-anagram/mand2.py | 33 ++++++++++++++++++++++++ valid-palindrome/mand2.py | 26 +++++++++++++++++++ 6 files changed, 124 insertions(+) create mode 100644 .gitignore create mode 100644 best-time-to-buy-and-sell-stock/mand2.py create mode 100644 contains-duplicate/mand2.py create mode 100644 two-sum/mand2.py create mode 100644 valid-anagram/mand2.py create mode 100644 valid-palindrome/mand2.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..f55587475 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# Byte-co +# IntelliJ +/.idea/ +/.idea/.gitignore diff --git a/best-time-to-buy-and-sell-stock/mand2.py b/best-time-to-buy-and-sell-stock/mand2.py new file mode 100644 index 000000000..8535209d9 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/mand2.py @@ -0,0 +1,20 @@ +# 문제: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ + +def solution(prices): + profit, buy = 0, prices[0] + for price in prices: + diff = price - buy + buy = min(price, buy) + profit = max(profit, diff) + return profit + + +# 시간복잡도: O(n) +# 공간복잡도: O(1) + + +answer_1 = solution([7, 1, 5, 3, 6, 4]) +answer_2 = solution([7, 6, 4, 3, 1]) + +print(answer_1 == 5) +print(answer_2 == 0) diff --git a/contains-duplicate/mand2.py b/contains-duplicate/mand2.py new file mode 100644 index 000000000..9e918aa19 --- /dev/null +++ b/contains-duplicate/mand2.py @@ -0,0 +1,16 @@ +# 문제: https://leetcode.com/problems/contains-duplicate/ +def containsDuplicate(nums) -> bool: + done = set() + for num in nums: + if num in done: + return True + done.add(num) + return False + + +# 시간복잡도: O(n) +# 공간복잡도: O(n) + +print((containsDuplicate([1, 2, 3, 1]) is True)) +print((containsDuplicate([1, 2, 3, 4]) is False)) +print((containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2]) is True)) diff --git a/two-sum/mand2.py b/two-sum/mand2.py new file mode 100644 index 000000000..d0ce420fa --- /dev/null +++ b/two-sum/mand2.py @@ -0,0 +1,25 @@ +# 문제: https://leetcode.com/problems/two-sum/description/ + +# targetNum - list[i] 값이 list에 있는지 확인만 하면 끝. -> 이 아니고 i. j 리턴 +def solutions(nums, target_num): + table = {num: idx for idx, num in enumerate(nums)} + + for i, value in enumerate(nums): + look = target_num - value + print('look:', look) + # value -> idx로 바로 치환하기가 어렵.. + if look in table and i != table[look]: + look_idx = table[look] + return [i, look_idx] + + +# 시간복잡도: O(n) +# 공간복잡도: O(n) + +answer_1 = solutions([2, 7, 11, 15], 9) +answer_2 = solutions([3, 3], 6) # 중복된수가나오면..?!?!?!?! +answer_3 = solutions([3, 2, 4], 6) + +print(answer_1 == [0, 1]) +print(answer_2 == [0, 1]) +print(answer_3 == [1, 2]) diff --git a/valid-anagram/mand2.py b/valid-anagram/mand2.py new file mode 100644 index 000000000..5b9f4f5d9 --- /dev/null +++ b/valid-anagram/mand2.py @@ -0,0 +1,33 @@ +# 문제: https://leetcode.com/problems/valid-anagram/ + +# 풀이: s 와 t 에 입력된 알파벳의 갯수를 체크한다. ... +# 애너그램이면 T +def is_anagram(s, t) -> bool: + # 글자수가 같다는 조건이 없음. + if len(s) != len(t): + return False + + word_counter = {} + # s 문자열 분해 + for alpha in s: + # 초기화 + if alpha not in word_counter: + word_counter[alpha] = 0 + word_counter[alpha] += 1 + + for beta in t: + if beta not in word_counter: + return False + return True + + +# 시간복잡도: O(n) +# 공간복잡도: O(n) + +tc_1 = is_anagram("anagram", "nagaram") is True +tc_2 = is_anagram("rat", "car") is False +tc_3 = is_anagram("a", "ab") is False + +print(tc_1) +print(tc_2) +print(tc_3) diff --git a/valid-palindrome/mand2.py b/valid-palindrome/mand2.py new file mode 100644 index 000000000..88aac35e1 --- /dev/null +++ b/valid-palindrome/mand2.py @@ -0,0 +1,26 @@ +# 문제: https://leetcode.com/problems/valid-palindrome/description/ + +def solution(sentence): + # removing all non-alphanumeric characters (숫자는 ㅇㅋ) + selected = '' + for s in sentence: + if s.isalnum(): + selected += s.lower() + return selected == selected[::-1] + + +# 시간복잡도: O(n) +# 공간복잡도: O(n) + + +answer_1 = solution("A man, a plan, a canal: Panama") +answer_2 = solution("0P") +answer_3 = solution("race a car") +answer_4 = solution(" ") +answer_5 = solution("a") + +print(answer_1 is True) +print(answer_2 is False) +print(answer_3 is False) +print(answer_4 is True) +print(answer_5 is True) From 59ba05ec719664b37d4dd2a8b61a72c8be08e27f Mon Sep 17 00:00:00 2001 From: mand2 Date: Fri, 3 May 2024 09:21:49 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[mand2]=20.gitignore=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index f55587475..000000000 --- a/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -# Byte-co -# IntelliJ -/.idea/ -/.idea/.gitignore From c63d0ca759a1ab72a7b3ac4ed844f6f20a878c03 Mon Sep 17 00:00:00 2001 From: mand2 Date: Fri, 3 May 2024 15:49:17 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[mand2]=20=EC=88=98=EC=A0=95:=20anagram,=20?= =?UTF-8?q?two-sum=20-=20two-sum:=20=EC=93=B8=EB=AA=A8=EC=97=86=EB=8A=94?= =?UTF-8?q?=20=EB=A1=9C=EA=B7=B8=20=EC=82=AD=EC=A0=9C=20-=20anagram:=20=20?= =?UTF-8?q?=20=20=20-=20=EC=B4=88=EA=B8=B0=ED=99=94=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=20=20=20=20=20-=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- two-sum/mand2.py | 1 - valid-anagram/mand2.py | 21 +++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/two-sum/mand2.py b/two-sum/mand2.py index d0ce420fa..c0398d20a 100644 --- a/two-sum/mand2.py +++ b/two-sum/mand2.py @@ -6,7 +6,6 @@ def solutions(nums, target_num): for i, value in enumerate(nums): look = target_num - value - print('look:', look) # value -> idx로 바로 치환하기가 어렵.. if look in table and i != table[look]: look_idx = table[look] diff --git a/valid-anagram/mand2.py b/valid-anagram/mand2.py index 5b9f4f5d9..aab87a188 100644 --- a/valid-anagram/mand2.py +++ b/valid-anagram/mand2.py @@ -1,4 +1,6 @@ # 문제: https://leetcode.com/problems/valid-anagram/ +from collections import defaultdict + # 풀이: s 와 t 에 입력된 알파벳의 갯수를 체크한다. ... # 애너그램이면 T @@ -7,19 +9,16 @@ def is_anagram(s, t) -> bool: if len(s) != len(t): return False - word_counter = {} + word_counter = defaultdict(int) # s 문자열 분해 for alpha in s: - # 초기화 - if alpha not in word_counter: - word_counter[alpha] = 0 word_counter[alpha] += 1 for beta in t: - if beta not in word_counter: + if beta not in word_counter or word_counter[beta] == 0: return False - return True - + word_counter[beta] -= 1 + return True # 시간복잡도: O(n) # 공간복잡도: O(n) @@ -27,7 +26,9 @@ def is_anagram(s, t) -> bool: tc_1 = is_anagram("anagram", "nagaram") is True tc_2 = is_anagram("rat", "car") is False tc_3 = is_anagram("a", "ab") is False +tc_4 = is_anagram("aacc", "ccac") is False -print(tc_1) -print(tc_2) -print(tc_3) +print('tc_1', tc_1) +print('tc_2', tc_2) +print('tc_3', tc_3) +print('tc_4', tc_4)