Skip to content
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

[yapp][mand2] 2주차 답안 제출 #56

Merged
merged 2 commits into from
May 11, 2024
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
37 changes: 37 additions & 0 deletions invert-binary-tree/mand2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### Intuition
- 재귀호출

### Approach
- 재귀 호출
- 마지막에 left<>right 이동

### Complexity
- Time complexity: O(n)
- Space complexity: O(n)


# Code

```python
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None

left = self.invertTree(root.left)
right = self.invertTree(root.right)

root.left = right
root.right = left

return root


```
36 changes: 36 additions & 0 deletions linked-list-cycle/mand2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Intuition

### Approach
- pos 는 입력값이 아니다.
- 계속 회전하므로(detour), 언젠가는 one_way 로 가는 값과 만나게 됨.

### Complexity
- Time complexity: O(n)
- Space complexity: O(1)


### Code

```python
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None

class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
if not head or not head.next:
return False

one_way = head
detour = head.next

while detour and detour.next:
if one_way == detour:
return True
one_way = one_way.next
detour = detour.next.next

return False
```
41 changes: 41 additions & 0 deletions merge-two-sorted-lists/mand2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
### Intuition

### Approach

### Complexity
- Time complexity: O(n)
- Space complexity: O(1)

### Code

```python
# 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, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
if not l1:
return l2
if not l2:
return l1

merged_list = ListNode()
current = merged_list

while l1 and l2:
if l1.val <= l2.val:
current.next = l1
l1 = l1.next
else:
current.next = l2
l2 = l2.next

current = current.next # pointer setting.

current.next = l1 if l1 else l2

return merged_list.next
```
37 changes: 37 additions & 0 deletions reverse-linked-list/mand2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### Intuition
- 투 포인터 (prev, curr)

### Approach
- 투 포인터
- 포인터를 이동하기 전에 prev 가 가리키는 (.next) 를 reverse 해 주어야 한다.

### Complexity
- Time complexity: O(n)
- Space complexity: O(1)


### Code

```python
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head

prev, curr = None, head

while curr:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node

return prev

```
42 changes: 42 additions & 0 deletions valid-parentheses/mand2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
### Intuition
- opening(`([{`) 스택을 쌓는다.
- 비교한다.

### Approach
1. 먼저 입력받은 글자의 길이가 짝수가 아니면 무조건 **False**.
2. for 문
- opening(`([{`) 이면 stack 에 넣는다.
- stack 이 비어있거나, 짝이 맞지 않으면(`is_parentheses()==False`) **False**.
3. 다 완료되고 나서, 스택이 비어있다면 **True**.


### Complexity
- Time complexity: O(n)
- Space complexity: O(n)


### Code

```python
class Solution:
def isValid(self, s: str) -> bool:
stack = []
if len(s) % 2 != 0:
return False
for word in s:
if word in '([{':
stack.append(word)
elif not stack or is_parentheses(stack.pop(), word) is False:
return False
return not stack


def is_parentheses(pointer, word) -> bool:
if pointer == '(' and word == ')':
return True
elif pointer == '[' and word == ']':
return True
elif pointer == '{' and word == '}':
return True
else: return False
```