-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
[Chaedie] Week 8 #964
Changes from all commits
3cd4cc7
559a27b
175a780
3495323
a4b1982
daa1641
df919d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
한번에 풀지 못해 다시 풀어볼 예정입니다. | ||
|
||
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) | ||
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. 공간 복잡도 설명을 부탁드려도 될까요? 제 생각엔 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. O(V) 만큼의 call stack 사용 이라고 생각해 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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
""" | ||
Solution: 재귀를 활용한 풀이, memo 를 활용한 시간복잡도 개선 | ||
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. 열정이 있으신 것 같아서 팁을 드리자면, tabulation (흔히 bottom-up라고 부르는..)을 이용하면 좀 더 공간 효율적인 풀이가 가능합니다 |
||
|
||
Time: O(m * n) | ||
Space: O(2 * m * n) | ||
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. 일반적으로 Big-O 표기를 할 땐 상수곱을 생략합니다 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. 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) |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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번의 연산) | ||
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. 이것도 마찬가지로 O(n)이라고 적어주시는게 맞을 것 같습니다 |
||
Space: O(n) (모든 원소가 set에 들어갈 경우) | ||
""" | ||
|
||
|
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 |
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 |
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.
열정..! 👍