Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sun]WEEK 2 Solution #348

Merged
merged 7 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
TC: O(n)
SC: O(n)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공간 복잡도를 구하실 때 재귀 함수에서 매번 새로운 inorder 배열을 복제하는 부분을 고려하신 걸까요?

Copy link
Contributor Author

@sun912 sun912 Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아니요 잘못 파악했어요!!
달레님이 제공해주신 마약을 보고 깨달았습니다!
인덱스를 넘겨주면 SC 관점에서 좀 더 개선할 수 있다는 점요=)

"""
# Definition for a binary tree node.
# 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]:
if inorder:
idx = inorder.index(preorder.pop(0))
node = TreeNode(inorder[idx])
node.left = self.buildTree(preorder, inorder[0:idx])
node.right = self.buildTree(preorder, inorder[idx+1:])

return node
18 changes: 18 additions & 0 deletions counting-bits/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
TC: O(n)
SC: O(n)
"""


class Solution:
def countBits(self, n: int) -> List[int]:
dp = [0] * (n+1)
offset = 1

for i in range(1, n+1):
if offset * 2 == i:
offset = i
dp[i] = 1 + dp[i-offset]
return dp


24 changes: 24 additions & 0 deletions decode-ways/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
TC: O(n)
SC: O(n)

DNF, read solutions by Dale...ㅠㅠ
"""

class Solution:
def numDecodings(self, s: str) -> int:
memo = {len(s): 1}

def dfs(start):
if start in memo:
return memo[start]

if s[start] == "0":
return 0
if start + 1 < len(s) and int(s[start: start+2]) < 27:
memo[start] = dfs(start + 1) + dfs(start + 2)
else:
memo[start] = dfs(start + 1)
return memo[start]

return dfs(0)
30 changes: 30 additions & 0 deletions encode-and-decode-strings/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
TC: O(n)

"""
class Solution:
"""
@param: strs: a list of strings
@return: encodes a list of strings to a single string.
"""
def encode(self, strs):
result = ""
for str in strs:
result += str(len(str)) + "#" + str
return result

"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(self, str):
result = []
idx = 0
while idx < len(str):
temp_idx = idx
while str[temp_idx] != "#":
temp_idx += 1
length = int(str[idx:temp_idx])
result.append(str[temp_idx+1:temp_idx+length+1])
idx = temp_idx + length + 1
return result
16 changes: 16 additions & 0 deletions valid-anagram/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False

count_s = {}
count_t = {}

for i in range(len(s)):
count_s[s[i]] = 1 + count_s.get(s[i], 0)
count_t[t[i]] = 1 + count_t.get(t[i], 0)

for c in count_t:
if count_t[c] != count_s.get(c, 0):
return False
return True
Loading