-
-
Notifications
You must be signed in to change notification settings - Fork 195
[KwonNayeon] Week 2 #716
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
[KwonNayeon] Week 2 #716
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4eb4431
Create KwonNayeon.py
KwonNayeon 597d425
Merge branch 'main' of https://github.com/KwonNayeon/leetcode-study
KwonNayeon 9a78a48
Add placeholder file for 'Valid Anagram' problem
KwonNayeon a636a67
Solved 218. Valid Anagram using Python code
KwonNayeon 18e7dfa
Solved 230. Climbing Stairs using Python code
KwonNayeon 0e4d1ef
Solved 241. 3Sum using Python code
KwonNayeon 91937ec
Solved 253. Construct Binary Tree From Preorder And Inorder Traversal…
KwonNayeon 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,42 @@ | ||
""" | ||
Constraints: | ||
1. 3 <= nums.length <= 3000 | ||
2. -10^5 <= nums[i] <= 10^5 | ||
|
||
Time Complexity: | ||
- O(n^2) (정렬은 O(n log n), 이중 반복문은 O(n^2)) | ||
Space Complexity: | ||
- O(n) (결과 리스트) | ||
""" | ||
|
||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
nums.sort() | ||
result = [] | ||
|
||
for i in range(len(nums) - 2): | ||
if i > 0 and nums[i] == nums[i-1]: | ||
continue | ||
|
||
left, right = i+1, len(nums)-1 | ||
|
||
while left < right: | ||
sum = nums[i] + nums[left] + nums[right] | ||
|
||
if sum == 0: | ||
result.append([nums[i], nums[left], nums[right]]) | ||
|
||
while left < right and nums[left] == nums[left+1]: | ||
left += 1 | ||
while left < right and nums[right] == nums[right-1]: | ||
right -= 1 | ||
|
||
left += 1 | ||
right -= 1 | ||
|
||
elif sum < 0: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return result |
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,23 @@ | ||
""" | ||
Constraints: | ||
- 1 <= n <= 45 | ||
|
||
Time Complexity: | ||
- O(n) | ||
Space Complexity: | ||
- O(n) | ||
""" | ||
|
||
class Solution: | ||
def climbStairs(self, n: int) -> int: | ||
if n == 1: | ||
return 1 | ||
if n == 2: | ||
return 2 | ||
|
||
dp = [0] * (n+1) | ||
dp[1], dp[2] = 1, 2 | ||
|
||
for i in range(3, n+1): | ||
dp[i] = dp[i-1] + dp[i-2] | ||
return dp[n] |
28 changes: 28 additions & 0 deletions
28
construct-binary-tree-from-preorder-and-inorder-traversal/KwonNayeon.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,28 @@ | ||
""" | ||
Constraints: | ||
1. 1 <= preorder.length <= 3000 | ||
2. inorder.length == preorder.length | ||
3. -3000 <= preorder[i], inorder[i] <= 3000 | ||
4. preorder and inorder consist of unique values | ||
5. Each value of inorder also appears in preorder | ||
6. preorder is guaranteed to be the preorder traversal of the tree | ||
7. inorder is guaranteed to be the inorder traversal of the tree | ||
|
||
Time Complexity: | ||
- O(N^2). 각 노드(N)마다 inorder에서 index를 찾는 연산(N)이 필요하고, 각 노드를 한 번씩 방문하여 트리를 구성하기 때문. | ||
Space Complexity: | ||
- O(N). 재귀 호출 스택을 위한 공간이 필요하며, 최악의 경우(한쪽으로 치우친 트리) 재귀 깊이가 N까지 갈 수 있기 때문. | ||
""" | ||
|
||
class Solution: | ||
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
if not preorder or not inorder: | ||
return None | ||
|
||
root = TreeNode(preorder[0]) | ||
mid = inorder.index(preorder[0]) | ||
|
||
root.left = self.buildTree(preorder[1:mid+1], inorder[:mid]) | ||
root.right = self.buildTree(preorder[mid+1:], inorder[mid+1:]) | ||
|
||
return root |
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 |
---|---|---|
@@ -1 +1,21 @@ | ||
""" | ||
Constraints: | ||
- 1 <= len(s), len(t) <= 50_000 | ||
- s and t consist of lowercase English letters (a-z) only | ||
|
||
Time Complexity: | ||
- O(n log n) | ||
Space Complexity: | ||
- O(n) | ||
""" | ||
|
||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
|
||
s = s.replace(' ', '').lower() | ||
t = t.replace(' ', '').lower() | ||
|
||
if sorted(s) == sorted(t): | ||
return True | ||
else: | ||
return False |
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.
파이썬과 재귀함수가 조합이 잘 된것 같습니다!
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.
@TonyKim9401 님 리뷰 감사합니다!