-
Notifications
You must be signed in to change notification settings - Fork 126
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
[kayden] Week 08 Solutions #511
Changes from all commits
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,28 @@ | ||
from typing import Optional | ||
from collections import deque | ||
|
||
|
||
class Solution: | ||
# 시간복잡도: O(N) node 개수: N | ||
# 공간복잡도: O(N) | ||
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']: | ||
if node: | ||
visited = {} | ||
copy = Node(val=node.val) | ||
visited[copy.val] = copy | ||
q = deque() | ||
q.append((copy, node)) | ||
|
||
while q: | ||
cur, node = q.popleft() | ||
|
||
for idx, next_node in enumerate(node.neighbors): | ||
if next_node.val not in visited: | ||
new = Node(val=next_node.val) | ||
visited[new.val] = new | ||
q.append((new, next_node)) | ||
cur.neighbors.append(visited[next_node.val]) | ||
|
||
return copy | ||
|
||
return node |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,33 @@ | ||
# 시간복잡도: O(N) | ||
# 공간복잡도: O(N) | ||
class Solution: | ||
def longestConsecutive(self, nums: List[int]) -> int: | ||
nums = set(nums) | ||
answer = 0 | ||
# 시간복잡도: O(A*B) | ||
# 공간복잡도: O(A*B) | ||
def longestCommonSubsequence(self, text1: str, text2: str) -> int: | ||
a = len(text1) | ||
b = len(text2) | ||
|
||
for num in nums: | ||
if num - 1 not in nums: | ||
length = 1 | ||
lcs = [[0]*(b+1) for _ in range(a+1)] | ||
|
||
while num + length in nums: | ||
length += 1 | ||
for i in range(1, a+1): | ||
for j in range(1, b+1): | ||
if text1[i-1] == text2[j-1]: | ||
lcs[i][j] = lcs[i-1][j-1] + 1 | ||
lcs[i][j] = max(lcs[i][j], lcs[i-1][j], lcs[i][j-1]) | ||
|
||
answer = max(answer, length) | ||
return lcs[a][b] | ||
|
||
return answer | ||
# 시간복잡도: O(A*B) | ||
# 공간복잡도: O(A) | ||
def longestCommonSubsequence2(self, text1: str, text2: str) -> int: | ||
n = len(text1) | ||
lcs = [0]*n | ||
longest = 0 | ||
for ch in text2: | ||
cur = 0 | ||
for i in range(n): | ||
if cur < lcs[i]: | ||
cur = lcs[i] | ||
elif ch == text1[i]: | ||
lcs[i] = cur + 1 | ||
longest = max(longest, cur+1) | ||
|
||
return longest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# 시간복잡도: O(N) | ||
# 공간복잡도: O(N) | ||
class Solution: | ||
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. 제가 저번에 폴더를 착각해서 혼동을 드렸네요.. |
||
def longestConsecutive(self, nums: List[int]) -> int: | ||
nums = set(nums) | ||
answer = 0 | ||
|
||
for num in nums: | ||
if num - 1 not in nums: | ||
length = 1 | ||
|
||
while num + length in nums: | ||
length += 1 | ||
|
||
answer = max(answer, length) | ||
|
||
return answer |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from collections import defaultdict | ||
|
||
class Solution: | ||
# 시간복잡도: O(N) | ||
# 공간복잡도: O(N) | ||
def characterReplacement(self, s: str, k: int) -> int: | ||
|
||
n = len(s) | ||
l, r = 0, 1 | ||
|
||
d = defaultdict(int) | ||
d[s[l]] += 1 | ||
|
||
longest = 1 | ||
while l < r and r < n: | ||
d[s[r]] += 1 | ||
if r-l+1 - max(d.values()) > k: | ||
d[s[l]] -= 1 | ||
l += 1 | ||
longest = max(longest, r-l+1) | ||
r += 1 | ||
|
||
return longest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution: | ||
# 시간복잡도: O(N+M) list1:N, list2: M | ||
# 공간복잡도: O(N+M) | ||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
merged = ListNode() | ||
cur = merged | ||
|
||
while list1 and list2: | ||
if list1.val < list2.val: | ||
cur.next = list1 | ||
list1 = list1.next | ||
else: | ||
cur.next = list2 | ||
list2 = list2.next | ||
temp = temp.next | ||
|
||
if list1: | ||
cur.next = list1 | ||
|
||
if list2: | ||
cur.next = list2 | ||
|
||
return merged.next |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution { | ||
// time: O(1) | ||
// space: O(1) | ||
public int getSum(int a, int b) { | ||
while (b != 0) { | ||
int tmp = (a & b) << 1; | ||
a = a ^ b; | ||
b = tmp; | ||
} | ||
return a; | ||
} | ||
} | ||
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. 궁금해서 찾아보았는데, 이 문제는 오히려 파이썬의 풀이가 Mask를 써야해서 더 복잡해질 수 있나보네요 물론 과거 스터디 풀이중에 이런게 있는걸 보고 두번 놀랐습니다 class Solution:
def getSum(self, a: int, b: int) -> int:
return add(a,b) 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. 넵 자바나 c++를 사용하는게 더 간편해 보여서 자바를 사용했습니다! 저도 리트코드에서 궁금해서 + 연산으로 제출해봤는데 제출이 되더라구요 ㅋㅋㅋㅋ 하지만, 문제 의도가 비트 연산인것같아서 오랜만에 공부했습니다 ㅋㅋㅋ |
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.
text2가 길어지는 경우 문제가 되지 않을까 싶었는데 잘 작동하네요
text2 전체 순회보다는 set등으로 중복 순회를 줄일 수 있지 않을까요?
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.
set을 어떻게 사용할 수 있나요??