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..c0398d20a --- /dev/null +++ b/two-sum/mand2.py @@ -0,0 +1,24 @@ +# 문제: 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 + # 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..aab87a188 --- /dev/null +++ b/valid-anagram/mand2.py @@ -0,0 +1,34 @@ +# 문제: https://leetcode.com/problems/valid-anagram/ +from collections import defaultdict + + +# 풀이: s 와 t 에 입력된 알파벳의 갯수를 체크한다. ... +# 애너그램이면 T +def is_anagram(s, t) -> bool: + # 글자수가 같다는 조건이 없음. + if len(s) != len(t): + return False + + word_counter = defaultdict(int) + # s 문자열 분해 + for alpha in s: + word_counter[alpha] += 1 + + for beta in t: + if beta not in word_counter or word_counter[beta] == 0: + return False + word_counter[beta] -= 1 + 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 +tc_4 = is_anagram("aacc", "ccac") is False + +print('tc_1', tc_1) +print('tc_2', tc_2) +print('tc_3', tc_3) +print('tc_4', tc_4) 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)