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

[김대의] 2주차 문제 올립니다. #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions week-02/invert-binary-tree/kimeodml.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Intuition

재귀를 활용해 오른쪽 노드와 왼쪽 노드를 교환

# Approach

1. 현재 node가 null일 경우 null을 반환
2. 현재 노드의 오른쪽/왼쪽을 서로 교환
3. inverTree를 재귀적으로 호출하여 하위 노드도 동일하게 교환
4. 루트 노드 반환

# Complexity

- 시간복잡도: O(n)
- 공간복잡도: O(n)

# Code
```js
var invertTree = function(root) {
if(!root) return null;

[root.right, root.left] = [root.left, root.right];
invertTree(root.right);
invertTree(root.left);
return root;
Copy link
Contributor

Choose a reason for hiding this comment

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

오 배열을 통한 swap

};
```
29 changes: 29 additions & 0 deletions week-02/linked-list-cycle/kimeodml.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Intuition

set을 이용해 중복 감지

# Approach

1. 빈 해시셋을 생성
2. 노드를 순회하면서 해시셋에 존재하면 true 아니면 false를 반환

# Complexity

- 시간복잡도: O(n)
- 공간복잡도: O(n)

# Code
```js
var hasCycle = function(head) {
let cur = head;
let node = new Set();
while(cur) {
if(node.has(cur)) {
return true;
}
node.add(cur);
cur = cur.next;
}
return false;
};
```
37 changes: 37 additions & 0 deletions week-02/merge-two-sorted-lists/kimeodml.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Intuition

두 개의 연결 리스트를 서로 비교하면서 이동

# Approach

1. 하나의 dummy 노드를 생성(리스트의 시작점)
2. list1과 list2가 존재할 때까지 반복하면서 서로의 값을 비교 후 연결
3. 남은 노드가 있을 경우 결과 리스트에 연결
4. 결과 리스트 반환

# Complexity

- 시간복잡도: O(n+m)
- 공간복잡도: O(1)

# Code
```js
var mergeTwoLists = function(list1, list2) {
let dummy = new ListNode();
let tail = dummy;

while(list1 && list2) {
if(list1.val >= list2.val) {
tail.next = list2;
list2 = list2.next;
} else {
tail.next = list1;
list1 = list1.next;
}
tail = tail.next;
}

tail.next = list1 || list2;
return dummy.next;
};
```
34 changes: 34 additions & 0 deletions week-02/reverse-linked-list/kimeodml.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Intuition

현재의 노드가 이전 노드를 가리키도록 변경

# Approach

1. cur 노드에 head를 prev 노드를 null로 초기화
2. cur 노드가 mull이 아닐 때까지 반복
2.1. 다음 노드를 cur에 저장
2.2. 다음 노드를 prev 노드로 변경(노드 연결 반전)
3.4. prev 노드를 현재 노드로 업데이트
2.3. 현재 노드를 다음 노드로 업데이트
3. 반전된 노드 반환


# Complexity

- 시간복잡도: O(n)
- 공간복잡도: O(1)

# Code
```js
var reverseList = function(head) {
let cur = head;
let prev = null;
while(cur) {
cur = head.next;
head.next = prev;
prev = head;
head = cur;
}
return prev;
Copy link
Contributor

Choose a reason for hiding this comment

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

저도 이렇게 했는데 단순한 과정인데 작성하기 힘들더라구여..

};
```
43 changes: 43 additions & 0 deletions week-02/valid-parentheses/kimeodml.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Intuition

스택을 활용해 괄호들을 비교 후 pop하거나 push함

# Approach

1. 빈 배열 arr 생성
2. 여는 괄호일 경우 arr에 push
3. 닫는 괄호일 경우
3.1. 이때 arr이 비어 있을 경우 false로 반환
3.2. 가장 맨 위의 prev와 닫는 괄호를 비교하여 올바르면 괄호 제거 그렇지 않으면 false로 반환
3. arr이 비어있으면 true 그렇지 않을 경우 false로 반환


# Complexity

- 시간복잡도: O(n)
- 공간복잡도: O(n)

# Code
```js
var isValid = function(s) {
let arr = [];
for (let char of s) {
if (char === '(' || char === '[' || char === '{') {
arr.push(char);
} else if (char === ')' || char === ']' || char === '}') {
if (arr.length === 0) {
return false;
}
let prev = arr[arr.length - 1];
if ((char === ']' && prev === '[') ||
(char === ')' && prev === '(') ||
(char === '}' && prev === '{')) {
arr.pop();
} else {
return false;
}
}
}
return arr.length === 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

오 스택이 비었는 지 확인을 단순하게 했네요 굳

};
```