-
-
Notifications
You must be signed in to change notification settings - Fork 195
[croucs] WEEK 2 #714
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
Merged
Merged
[croucs] WEEK 2 #714
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 @@ | ||
# 2Sum을 활용한 풀이 | ||
# Note : 정렬 후 푸는 것이 더 직관적이고, 빠름 | ||
|
||
""" | ||
복잡도 : 예상 -> 예상한 이유 | ||
|
||
시간 복잡도 : O(n^2) -> nums 배열 2중 for문 | ||
공간 복잡도 : O(n) -> 최악의 경우 nums 배열 길이만큼의 딕셔너리 생성 | ||
""" | ||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
ans_list = set() | ||
added_pair = {} | ||
for target_i, target_number in enumerate(nums): | ||
cur_ans_list = [] | ||
num_dict = {} | ||
if target_number in added_pair: | ||
continue | ||
|
||
for i in range(target_i + 1, len(nums)): | ||
num_A = nums[i] | ||
num_B = - target_number - num_A | ||
if num_B in num_dict: | ||
cur_ans_list.append(sorted([target_number, num_A, num_B])) | ||
added_pair[target_number] = num_A | ||
num_dict[num_A] = 1 | ||
for item in cur_ans_list: | ||
ans_list.add(tuple(item)) | ||
return list(ans_list) | ||
|
This file contains hidden or 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 @@ | ||
""" | ||
복잡도 : 예상 -> 예상한 이유 | ||
|
||
시간 복잡도 : O(n) -> 배열의 길이 n-2 만큼 반복하므로 | ||
공간 복잡도 : O(n) -> n 길이의 배열 하나를 생성하므로 | ||
""" | ||
class Solution: | ||
def climbStairs(self, n: int) -> int: | ||
if n == 1: | ||
return 1 | ||
elif n == 2: | ||
return 2 | ||
a = [0] * n | ||
a[0] = 1 | ||
a[1] = 2 | ||
for i in range(2, n): | ||
a[i] = a[i-1] + a[i-2] | ||
return a[n-1] | ||
|
59 changes: 59 additions & 0 deletions
59
construct-binary-tree-from-preorder-and-inorder-traversal/heypaprika.py
This file contains hidden or 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,59 @@ | ||
# 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 | ||
|
||
# preorder -> root, left, right | ||
# inorder -> left, root, right | ||
|
||
# Ex1 [3,9,20,null,null,15,7] | ||
# preorder -> root-1, left, (root-2 - root, left, right) | ||
# inorder -> left, root-1, (root-2 - left, root, right) | ||
|
||
# Ex2 [-1] | ||
# preorder -> root, left(x), right(x) | ||
# inorder -> left(x), root, right(x) | ||
|
||
# Ex3 [3,9,20,11,8,15,7] | ||
# preorder -> root-1, (root-2(left) - root, left, right), (root-3(right) - root, left, right) | ||
# [3,9,11,8,20,15,7] | ||
# inorder -> (root-1(left) - left, root, right), root-2, (root-3(right) - left, root, right) | ||
# [11, 9, 8, 3, 15, 20, 7] | ||
|
||
# Ex4 [3,9,20,11,8] | ||
# preorder -> root-1, (root-2(left) - root, left, right), (root-3(right) - root) | ||
# [3,9,11,8,20] | ||
# inorder -> (root-1(left) - left, root, right), root-2, (root-3(right) - left(X), root) | ||
# [11, 9, 8, 3, 20] | ||
|
||
# 문제풀이 : divide and conquer | ||
# preorder의 첫번째 요소를 가지고 inorder split | ||
# split된 left와 right의 개수에 맞게 preorder을 두 번 째 원소부터 순서대로 할당 | ||
# 예) left 원소 개수 : 3 -> preorder[1:1+3], right 원소 개수 : 2 -> preorder[1+3:] | ||
# left, right 각각 buildTree로 넣기 | ||
|
||
""" | ||
복잡도 : 예상 -> 예상한 이유 | ||
|
||
시간 복잡도 : O(n) -> 분할해서 처리하지만, 모든 노드를 방문하므로 | ||
공간 복잡도 : O(n) -> 입력 배열만큼의 길이와 거의 동일한 배열이 생성되므로 | ||
""" | ||
class Solution: | ||
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
if len(preorder) == 0: | ||
return None | ||
if len(preorder) == 1: | ||
return TreeNode(preorder[0]) | ||
root = TreeNode(preorder[0]) | ||
|
||
split_idx = inorder.index(preorder[0]) | ||
left_inorder = inorder[:split_idx] | ||
right_inorder = inorder[split_idx+1:] | ||
left_preorder = preorder[1:len(left_inorder)+1] | ||
right_preorder = preorder[len(left_inorder)+1:] | ||
root.left = self.buildTree(left_preorder, left_inorder) | ||
root.right = self.buildTree(right_preorder, right_inorder) | ||
return root | ||
Comment on lines
+43
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inorder과 preorder의 관계를 사용하여 dfs 풀이도 참조하시면 문제 이해에 더 도움이 되시지 않을까 싶습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인덱스만 가지고 할 수도 있었군요..! 참조해주셔서 감사합니다! |
||
|
This file contains hidden or 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) -> n+1 길이의 배열 하나를 생성하므로 | ||
""" | ||
class Solution: | ||
def numDecodings(self, s: str) -> int: | ||
if s[0] == '0': | ||
return 0 | ||
a = [0] * (len(s)+1) | ||
a[0] = 1 | ||
a[1] = 1 | ||
for i in range(2, len(s)+1): | ||
if 1 <= int(s[i-1]) <= 9: | ||
a[i] += a[i-1] | ||
if 10 <= int(s[i-2:i]) <= 26: | ||
a[i] += a[i-2] | ||
return a[-1] | ||
|
This file contains hidden or 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 @@ | ||
""" | ||
복잡도 : 예상 -> 예상한 이유 | ||
|
||
시간 복잡도 : O(n) -> 배열의 길이 만큼 연산하기 때문 | ||
공간 복잡도 : O(1)? -> 생성한 count_list 배열이 상수이기 때문 | ||
""" | ||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
count_list = [0] * 26 | ||
for s_char in s: | ||
count_list[ord(s_char) - ord('a')] += 1 | ||
for t_char in t: | ||
count_list[ord(t_char) - ord('a')] -= 1 | ||
for count in count_list: | ||
if count != 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.
Uh oh!
There was an error while loading. Please reload this page.