Skip to content

[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 7 commits into from
Dec 21, 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
42 changes: 42 additions & 0 deletions 3sum/KwonNayeon.py
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
23 changes: 23 additions & 0 deletions climbing-stairs/KwonNayeon.py
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]
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:])
Comment on lines +19 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

파이썬과 재귀함수가 조합이 잘 된것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TonyKim9401 님 리뷰 감사합니다!


return root
20 changes: 20 additions & 0 deletions valid-anagram/KwonNayeon.py
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
Loading