-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #355 from kjb512/main
[kayden] Week 02 Solutions
- Loading branch information
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
construct-binary-tree-from-preorder-and-inorder-traversal/kayden.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |