Skip to content

[Chaedie] Week 8 #964

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
Feb 2, 2025
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
32 changes: 32 additions & 0 deletions clone-graph/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
한번에 풀지 못해 다시 풀어볼 예정입니다.
Copy link
Contributor

Choose a reason for hiding this comment

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

열정..! 👍


Solution:
1) oldToNew 라는 dict 를 만들어 무한루프를 방지합니다.
2) newNode = Node(node.val), newNode.neighbors.append(dfs(nei)) 를 통해 clone을 구현합니다.

정점 V, 간선 E
Time: O(V + E)
Space: O(V + E)
Copy link
Contributor

Choose a reason for hiding this comment

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

공간 복잡도 설명을 부탁드려도 될까요? 제 생각엔 O(V)같아서요 :) 채디님 생각이 궁금합니다

Copy link
Contributor Author

Choose a reason for hiding this comment

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

O(V) 만큼의 call stack 사용
O(E) 만큼의 oldToNew 의 value (neighbors) 저장

이라고 생각해 O(V + E) 라고 생각했습니다..!

"""


class Solution:
def cloneGraph(self, root: Optional["Node"]) -> Optional["Node"]:
if not root:
return None
oldToNew = {}

def dfs(node):
if node in oldToNew:
return oldToNew[node]

newNode = Node(node.val)
oldToNew[node] = newNode

for nei in node.neighbors:
newNode.neighbors.append(dfs(nei))

return newNode

return dfs(root)
25 changes: 25 additions & 0 deletions longest-common-subsequence/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Solution: 재귀를 활용한 풀이, memo 를 활용한 시간복잡도 개선
Copy link
Contributor

Choose a reason for hiding this comment

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

열정이 있으신 것 같아서 팁을 드리자면, tabulation (흔히 bottom-up라고 부르는..)을 이용하면 좀 더 공간 효율적인 풀이가 가능합니다
고민해보시거나, 이번 기수 저번 기수 다른 분들 풀이를 참고해주세요!


Time: O(m * n)
Space: O(2 * m * n)
Copy link
Contributor

Choose a reason for hiding this comment

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

일반적으로 Big-O 표기를 할 땐 상수곱을 생략합니다
O(2 * m * n) -> O(m * n)
참고: #445

Copy link
Contributor

Choose a reason for hiding this comment

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

2도 어떻게 곱하게 되신건지 여쭤볼 수 있을까요 :)

"""


class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
memo = {}

def dfs(i, j):
if (i, j) in memo:
return memo[(i, j)]

if i == len(text1) or j == len(text2):
memo[(i, j)] = 0
elif text1[i] == text2[j]:
memo[(i, j)] = 1 + dfs(i + 1, j + 1)
else:
memo[(i, j)] = max(dfs(i + 1, j), dfs(i, j + 1))
return memo[(i, j)]

return dfs(0, 0)
31 changes: 31 additions & 0 deletions longest-repeating-character-replacement/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Solution:
1) sliding window 활용
2) window 에 들어가는 글자들은 counts 라는 hash map 으로 갯수를 세어준다.
3) 순회를 하면서 window size 가 counts에서 가장 큰숫자 + k 보다 크면 invalid window 이기에 left 글자 갯수를 빼고 포인터를 이동해준다.
4) max_count 는 기존 max 값과 현재 window 사이즈 중 큰 숫자를 셋해준다.

Time: O(n) = O(26n)
Space: O(1) = O(26)
"""


class Solution:
def characterReplacement(self, s: str, k: int) -> int:
counts = defaultdict(int)
max_count = 0

l, r = 0, 0
while l <= r and r < len(s):
counts[s[r]] += 1
window_size = r - l + 1

if window_size > max(counts.values()) + k:
counts[s[l]] -= 1
l += 1

window_size = r - l + 1
max_count = max(window_size, max_count)
r += 1

return max_count
2 changes: 1 addition & 1 deletion longest-substring-without-repeating-characters/Chaedie.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
1) for iteration 을 도는 동안 hash set 을 이용해 처음 발견한 원소들을 window set에 넣는다.
2) 중복되는 원소를 발견할 경우 해당 원소의 중복이 사라질때까지 left side 의 원소들을 하나씩 제거한다.

Time: O(n^2) = O(n) (for iteration) * O(n) 최악의 경우 n만큼의 중복제거
Time: O(2n) = O(n) (최악의 경우 n만큼의 중복제거) * O(2) (원소당 최대 2번의 연산)
Copy link
Contributor

Choose a reason for hiding this comment

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

이것도 마찬가지로 O(n)이라고 적어주시는게 맞을 것 같습니다

Space: O(n) (모든 원소가 set에 들어갈 경우)
"""

Expand Down
20 changes: 20 additions & 0 deletions number-of-1-bits/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Solution:
1) 숫자에 1 bit 을 and 연산 결과가 1일 경우 result 에 1을 더한다.
2) n 을 오른쪽으로 쉬프팅 시킨다.

N: n의 bit 수
Time: O(N)
Space: O(1)

"""


class Solution:
def hammingWeight(self, n: int) -> int:
result = 0
while n > 0:
if n & 1:
result += 1
n = n >> 1
return result
8 changes: 4 additions & 4 deletions reverse-linked-list/Chaedie.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
Solution:
1) next node를 저장합니다.
1) temp node를 저장합니다.
2) cur node 를 prev node 로 연결시킵니다.
3) cur node 가 prev node 가 됩니다.
4) cur node 는 next node 가 됩니다.
4) cur node 는 temp node 가 됩니다.
Time: O(n)
Space: O(1)
"""
Expand All @@ -14,8 +14,8 @@ def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
cur = head
while cur:
next = cur.next
temp = cur.next
cur.next = prev
prev = cur
cur = next
cur = temp
return prev
18 changes: 18 additions & 0 deletions sum-of-two-integers/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Solution: 해설을 활용한 풀이입니다.
1) xor 로 더하기를 만들 수있다.
2) & << 1 로 carry 를 만들 수 있다.
3) python 의 경우 32비트 마스크를 사용해 음수 케이스를 고려한다.

"""


class Solution:
def getSum(self, a: int, b: int) -> int:
mask = 0xFFFFFFFF

xor = a ^ b
carry = (a & b) << 1
while carry & mask:
xor, carry = xor ^ carry, (xor & carry) << 1
return (xor & mask) if carry > 0 else xor
22 changes: 21 additions & 1 deletion unique-paths/Chaedie.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def uniquePaths(self, m: int, n: int) -> int:
dp = []
for i in range(m):
dp.append([0] * n)
print(dp)

for i in range(m):
for j in range(n):
Expand All @@ -31,3 +30,24 @@ def uniquePaths(self, m: int, n: int) -> int:
dp[i][j] = up_value + left_value

return dp[m - 1][n - 1]


"""
* 7주차에 받은 리뷰사항 뒤늦게 반영합니다.
Solution:
1) dp를 1로 m*n 배열로 초기화
2) 순회를 1부터 m, 1부터 n까지로 변경
3) up_value, left_value 를 Out of bounds 처리 없이 dp 배열로 계산

"""


class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp = [[1] * n] * m

for i in range(1, m):
for j in range(1, n):
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]

return dp[m - 1][n - 1]