diff --git a/3sum/kayden.py b/3sum/kayden.py new file mode 100644 index 000000000..380e9a33b --- /dev/null +++ b/3sum/kayden.py @@ -0,0 +1,62 @@ +class Solution: + + # 해시맵 + # 시간복잡도: O(N^2) + # 공간복잡도: O(N) + def threeSum(self, nums: List[int]) -> List[List[int]]: + n = len(nums) + nums.sort() + + check = {} + for idx, num in enumerate(nums): + check[num] = idx + + answer = set() + for i in range(n-2): + if nums[i] > 0: + break + if i > 0 and nums[i] == nums[i-1]: + continue + for j in range(i + 1, n): + target = -(nums[i] + nums[j]) + if not check.get(target): + continue + if j >= check[target]: + continue + + answer.add((nums[i], nums[j], target)) + + return list(answer) + + # 투포인터 + # 시간복잡도: O(N^2) + # 공간복잡도: O(N) + def threeSum2(self, nums: List[int]) -> List[List[int]]: + n = len(nums) + nums.sort() + + answer = set() + for i in range(n-2): + if nums[i] > 0: + break + if i > 0 and nums[i] == nums[i-1]: + continue + l, r = i+1, n-1 + while l -nums[i]: + r -= 1 + + return list(answer) diff --git a/best-time-to-buy-and-sell-stock/kayden.py b/best-time-to-buy-and-sell-stock/kayden.py new file mode 100644 index 000000000..4928b151d --- /dev/null +++ b/best-time-to-buy-and-sell-stock/kayden.py @@ -0,0 +1,15 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(1) +class Solution: + def maxProfit(self, prices: List[int]) -> int: + + answer = 0 + cur = float(inf) + for price in prices: + if cur < price: + answer = max(answer, price - cur) + + if price < cur: + cur = price + + return answer diff --git a/group-anagrams/kayden.py b/group-anagrams/kayden.py new file mode 100644 index 000000000..8e85bfc78 --- /dev/null +++ b/group-anagrams/kayden.py @@ -0,0 +1,10 @@ +# 시간복잡도: O(N*AlogA) A: 0<= strs[i]의 길이 <= 100 +# 공간복잡도: O(N) +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + groups = {} + for anagram in strs: + key = str(sorted(anagram)) + groups.setdefault(key, []).append(anagram) + + return list(groups.values()) diff --git a/implement-trie-prefix-tree/kayden.py b/implement-trie-prefix-tree/kayden.py new file mode 100644 index 000000000..0845a5902 --- /dev/null +++ b/implement-trie-prefix-tree/kayden.py @@ -0,0 +1,40 @@ +class Node: + + def __init__(self, ending=False): + self.children = {} + self.ending = ending + +# 공간복잡도: O(w*l) w: 단어 수 l: 단어의 평균 길이 +class Trie: + + def __init__(self): + self.head = Node(ending=True) + + # 시간복잡도: O(N) + def insert(self, word: str) -> None: + node = self.head + for ch in word: + if ch not in node.children: + node.children.setdefault(ch, Node()) + node = node.children[ch] + node.ending = True + + # 시간복잡도: O(N) + def search(self, word: str) -> bool: + node = self.head + for ch in word: + if ch not in node.children: + return False + node = node.children[ch] + + return node.ending + + # 시간복잡도: O(N) + def startsWith(self, prefix: str) -> bool: + node = self.head + for ch in prefix: + if ch not in node.children: + return False + node = node.children[ch] + + return True diff --git a/word-break/kayden.py b/word-break/kayden.py new file mode 100644 index 000000000..cf3fec88f --- /dev/null +++ b/word-break/kayden.py @@ -0,0 +1,25 @@ +# 시간복잡도: O(S*W) +# S: s의 길이 300 W: worDict 각 단어의 총 길이 20*1000 +# 300 * 20*1000 = 6*1e6 (600만) +# 공간복잡도: O(S) +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + memo = {} + def dfs(idx): + if idx in memo: + return memo[idx] + + if idx == len(s): + return True + + for word in wordDict: + l = len(word) + if s[idx:idx + l] == word: + if dfs(idx + l): + memo[idx] = True + return True + + memo[idx] = False + return False + + return dfs(0)