diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/kayden.py b/construct-binary-tree-from-preorder-and-inorder-traversal/kayden.py new file mode 100644 index 000000000..a30c09d9f --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/kayden.py @@ -0,0 +1,31 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(N) +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: + + mapping = {} + + for i in range(len(inorder)): + mapping[inorder[i]] = i + + preorder = collections.deque(preorder) + + def build(start, end): + if start > end: return None + + root = TreeNode(preorder.popleft()) + mid = mapping[root.val] + + root.left = build(start, mid - 1) + root.right = build(mid + 1, end) + + return root + + return build(0, len(preorder) - 1) diff --git a/counting-bits/kayden.py b/counting-bits/kayden.py new file mode 100644 index 000000000..77c831b66 --- /dev/null +++ b/counting-bits/kayden.py @@ -0,0 +1,10 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(N) +class Solution: + + def countBits(self, n: int) -> List[int]: + ans = [0 for _ in range(n + 1)] + for num in range(1, n + 1): + ans[num] = ans[num >> 1] + (num & 1) + + return ans diff --git a/decode-ways/kayden.py b/decode-ways/kayden.py new file mode 100644 index 000000000..076d04ae0 --- /dev/null +++ b/decode-ways/kayden.py @@ -0,0 +1,21 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(N) +class Solution: + def numDecodings(self, s: str) -> int: + if s[0] == "0": + return 0 + + n = len(s) + dp = [0] * (n + 1) + dp[0] = 1 + dp[1] = 1 + + for i in range(2, n + 1): + + if int(s[i - 1]) != 0: + dp[i] += dp[i - 1] + + if 10 <= int(s[i - 2:i]) <= 26: + dp[i] += dp[i - 2] + + return dp[n] diff --git a/encode-and-decode-strings/kayden.py b/encode-and-decode-strings/kayden.py new file mode 100644 index 000000000..336048beb --- /dev/null +++ b/encode-and-decode-strings/kayden.py @@ -0,0 +1,29 @@ +class Solution: + + # 시간복잡도: O(N) + # 공간복잡도: O(1) + def encode(self, strs): + res = "" + for str in strs: + size = len(str) + res += str(size) + res += str + + return res + + # 시간복잡도: O(N) + # 공간복잡도: O(N) + def decode(self, str): + idx = 0 + limit = len(str) + res = [] + + while idx < limit: + num = str[idx] + text = "" + for _ in range(num): + text += str[idx] + idx+=1 + res.append(text) + + return res diff --git a/valid-anagram/kayden.py b/valid-anagram/kayden.py new file mode 100644 index 000000000..136c89b1b --- /dev/null +++ b/valid-anagram/kayden.py @@ -0,0 +1,20 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(N) +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + counter_s = {} + for letter in s: + counter_s[letter] = counter_s.get(letter, 0) + 1 + + counter_t = {} + for letter in t: + counter_t[letter] = counter_t.get(letter, 0) + 1 + + for letter, cnt in counter_s.items(): + if counter_t.get(letter, 0) != cnt: + return False + + return True