Skip to content

Commit

Permalink
Merge pull request #716 from KwonNayeon/main
Browse files Browse the repository at this point in the history
[KwonNayeon] Week 2
  • Loading branch information
KwonNayeon authored Dec 21, 2024
2 parents 8f45cfd + 91937ec commit 8384b91
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
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:])

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

0 comments on commit 8384b91

Please sign in to comment.