-
-
Notifications
You must be signed in to change notification settings - Fork 245
[suwi] Week 04 #836
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
[suwi] Week 04 #836
Changes from 8 commits
666ad79
726459c
32e1e4c
333c656
7f4e8ad
5c4c12a
06701b1
929ae16
c2689a9
563bc02
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,33 @@ | ||
""" | ||
기억할 키워드 | ||
dummy.next 리턴해서 건너뜀 | ||
list1 list2 둘 중 하나가 None이 되면 while문 끝내고 나머지 next로 이어줌 | ||
|
||
list1의 길이 M, list2의 길이N | ||
|
||
TC : O(M + N) | ||
|
||
SC : O(1) | ||
|
||
추가적인 풀이 : 알고달레에서 재귀방법 확인 | ||
""" | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
dummy = ListNode() | ||
node = dummy | ||
|
||
while list1 and list2 : | ||
if list1.val < list2.val : | ||
node.next = ListNode(list1.val) | ||
list1 = list1.next | ||
else : | ||
node.next = ListNode(list2.val) | ||
list2 = list2.next | ||
node = node.next | ||
node.next = list1 or list2 | ||
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. 안녕하세요 @sungjinwi 님 덕분에 if 문 없이 |
||
return dummy.next |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
풀이 : 비지 않을경우 합 nSum을 구하고 list를 돌면서 빼고 남은 것이 답 | ||
|
||
|
||
TC : O(N) | ||
sum구할 떄 O(N) + for문 O(N) = O(2N)\ | ||
|
||
SC : O(1) | ||
""" | ||
|
||
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
nSum = sum(range(0,len(nums) + 1)) | ||
for num in nums : | ||
nSum -=num | ||
return nSum |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
풀이 : | ||
상하좌우 이동한 좌표가 board범위 벗어나면 False | ||
board[m][n]이 word[idx]와 불일치하면 False | ||
이미 방문했을경우 False | ||
단어가 완성되면 True | ||
상하좌우 한칸 이동한칸에 대해 재귀적 호출 | ||
상하좌우 중 True 있으면 True 없으면 False | ||
|
||
TC : O(M * N * 4 ^ W) | ||
board의 크기에 비례 -> M * N | ||
단어의 길이 만큼 상하좌우 재귀 호출 -> 4 ^ W | ||
|
||
SC : O(M * N + W) | ||
set의 메모리는 board 크기에 비례 -> M * N | ||
함수 호출 스택은 단어 길이에 비례 -> W | ||
""" | ||
|
||
class Solution: | ||
def exist(self, board: List[List[str]], word: str) -> bool: | ||
visit = set() | ||
|
||
def dfs(m: int, n: int, idx: int) -> bool: | ||
if not (0 <= m < row and 0 <= n < col): | ||
return False | ||
if not board[m][n] == word[idx]: | ||
return False | ||
if (m, n) in visit: | ||
return False | ||
if idx == len(word) - 1: | ||
return True | ||
|
||
visit.add((m, n)) | ||
if any (dfs(m + r, n + c, idx + 1) \ | ||
for (r, c) in [(1, 0), (-1, 0), (0, 1), (0, -1)]): | ||
return True | ||
|
||
visit.remove((m, n)) | ||
return False | ||
|
||
row = len(board) | ||
col = len(board[0]) | ||
|
||
for m in range(row): | ||
for n in range(col): | ||
if dfs(m, n, 0): | ||
return True | ||
return False |
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.
ListNode의 인스턴스를 새로 생성하기보다, 패러미터로 주어진 값들을 참조하는 방식의 풀이는 어떨까요?
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.
아 새로 안 만들고 바로 그냥 list1, list2를 할당해주면 되겠네요
어차피 next는 계속 업데이트되니까요! 감사합니다