-
Notifications
You must be signed in to change notification settings - Fork 54
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
[sun]WEEK 2 Solution #348
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
91e33fc
Week2. valid-anagram solution
e29c515
Merge remote-tracking branch 'upstream/main'
6543850
counting-bits, encode-and-decode-strings solved
sun912 20c0de5
Merge remote-tracking branch 'upstream/main'
sun912 5f779b6
added TC, SC
sun912 227601a
construct-binary-tree-from-preorder-and-inorder-traversal solution
sun912 4bff29b
decode-ways solution
sun912 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
construct-binary-tree-from-preorder-and-inorder-traversal/sun912.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,19 @@ | ||
""" | ||
TC: O(n) | ||
SC: O(n) | ||
""" | ||
# 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 |
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,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 | ||
|
||
|
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,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) |
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,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 |
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,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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
공간 복잡도를 구하실 때 재귀 함수에서 매번 새로운 inorder 배열을 복제하는 부분을 고려하신 걸까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아니요 잘못 파악했어요!!
달레님이 제공해주신 마약을 보고 깨달았습니다!
인덱스를 넘겨주면 SC 관점에서 좀 더 개선할 수 있다는 점요=)