Skip to content

[박종훈] 7주차 답안 제출 #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

Merged
merged 1 commit into from
Jun 15, 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
30 changes: 30 additions & 0 deletions binary-tree-level-order-traversal/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
- 문제: https://leetcode.com/problems/binary-tree-level-order-traversal/
- time complexity : O(n)
- space complexity : O(n), 트리가 균등한 형태일 경우 O(logn)에 가까워진다. (결과에 사용되는 list는 고려하지 않는다.)
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-102

```java
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();

dfs(result, root, 0);

return result;
}

public void dfs(List<List<Integer>> result, TreeNode node, int level) {
if(node == null) {
return;
}

if (result.size() <= level) {
result.add(new ArrayList<>());
}
result.get(level).add(node.val);

dfs(result, node.left, level + 1);
dfs(result, node.right, level + 1);
}
}
```
27 changes: 27 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- 문제: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
- time complexity : O(n)
- space complexity : O(n), 트리가 균등한 형태일 경우 O(logn)에 가까워진다.
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-235

```java
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
return dfs(root, p, q);
}

private TreeNode dfs(TreeNode node, TreeNode p, TreeNode q) {
if (node == null) {
return null;
}

TreeNode left = dfs(node.left, p, q);
TreeNode right = dfs(node.right, p, q);

if ((left != null && right != null) || node.val == p.val || node.val == q.val) {
return node;
}

return left != null ? left : right;
}
}
```
35 changes: 35 additions & 0 deletions remove-nth-node-from-end-of-list/dev-jonghoonpark.md
Copy link
Member

Choose a reason for hiding this comment

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

우선 선형적으로 길이를 구한 뒤 다시 처음부터 시작하여 length - n - 1 까지를 구해 타겟팅된 노드를 제거하는 전략이군요. 제가 생각지 못한 풀이법이라 새롭네요 ㅎㅎ

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
- 문제: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
- time complexity : O(n)
- space complexity : O(1)
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-19

```java
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode current = head;
int length = 0;
while(current != null) {
length++;
current = current.next;
}

if (length == 1) {
return null;
}

if (length == n) {
return head.next;
}

int removeIndex = length - n;
int pointer = 0;
current = head;
while (pointer < removeIndex - 1) {
current = current.next;
pointer++;
}
current.next = current.next.next;
return head;
}
}
```
92 changes: 92 additions & 0 deletions reorder-list/dev-jonghoonpark.md
Copy link
Member

Choose a reason for hiding this comment

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

보고 배우겠습니다. 🙇
저에게는 아직 어려운 풀이법이네요 하하

Copy link
Contributor Author

@dev-jonghoonpark dev-jonghoonpark Jun 10, 2024

Choose a reason for hiding this comment

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

포인터 방식은 저도 어려워서
한참 잡고있었네요 ㅎㅎ

공간 복잡도가 줄긴 하는데
직관적이지는 못하다고 생각됩니다 ㅎㅎ
(데이터가 엄청 많지 않은 이상 크게 차이도 없고, 오히려 손해일수도...)

Copy link
Contributor

Choose a reason for hiding this comment

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

포인터 없이 이렇게 풀어도 되는군요.. 저도 포인터는 쓸때마다 직관적으로 그려지지가 않는데 너무 흔하게 쓰이는 테크닉이라 버릴수도 없고 고민입니다 참고하도록 하겠습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
- 문제 : https://leetcode.com/problems/reorder-list/
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-143

## 방법 1 (list 사용)

- time complexity : O(n)
- space complexity : O(n)

```java
class Solution {
public void reorderList(ListNode head) {
List<ListNode> list = new ArrayList<>();

ListNode current = head;
while (current.next != null) {
list.add(current);
current = current.next;
}
list.add(current);
Copy link
Contributor

Choose a reason for hiding this comment

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

혹시 이 부분을 빼고 위에 while 조건을 current != null 만 하는 방법은 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

while (current != null) {
    list.add(current);
    current = current.next;
}

말씀해주신대로 이렇게 수정해도 잘 동작하네요...! : ) 좋을 것 같습니다...!


ListNode reordered = head;
for (int i = 1; i < list.size(); i++) {
if (i % 2 == 0) {
reordered.next = list.get(i / 2);
} else {
reordered.next = list.get(list.size() - ((i / 2) + 1));
}
reordered = reordered.next;
}
reordered.next = null;
}
}
```

## 방법 2 (pointer 사용)

- time complexity : O(n)
- space complexity : O(1)

```java
class Solution {
public void reorderList(ListNode head) {
ListNode prev = null;
ListNode slow = head;
ListNode fast = head;

while(slow != null && fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}

if (prev == null) {
return;
}

prev.next = null;

ListNode current = head;
ListNode reversed = reverse(slow);
while(true) {
ListNode temp = current.next;

if (reversed != null) {
current.next = reversed;
reversed = reversed.next;
current = current.next;
}

if (temp != null) {
current.next = temp;
current = current.next;
} else {
current.next = reversed;
break;
}
}
}

public ListNode reverse(ListNode treeNode) {
ListNode current = treeNode;
ListNode prev = null;
while(current != null) {
ListNode temp = current.next;
current.next = prev;
prev = current;
current = temp;
}
return prev;
}
}
```
27 changes: 27 additions & 0 deletions validate-binary-search-tree/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- 문제 : https://leetcode.com/problems/validate-binary-search-tree/
- time complexity : O(n)
- space complexity : O(n), 트리가 균등한 형태일 경우 O(logn)에 가까워진다.
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-98

```java
class Solution {
public boolean isValidBST(TreeNode root) {
return dfs(root.left, (long) Integer.MIN_VALUE - 1, root.val)
&& dfs(root.right, root.val, (long) Integer.MAX_VALUE + 1);
}

private boolean dfs(TreeNode node, long min, long max) {
if (node == null) {
return true;
}

// left로 갈 때 max를 자신으로, right로 갈 때는 min을 자신으로
if (!(dfs(node.left, min, node.val)
&& dfs(node.right, node.val, max))) {
return false;
}
Comment on lines +18 to +22
Copy link
Member

Choose a reason for hiding this comment

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

TreeNode끼리 비교하지 않고 Int 값으로 비교한 게 인상적이네요 ✨


return node.val > min && node.val < max;
}
}
```