Skip to content

[Tessa1217] Week 12 Solutions #1594

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 5 commits into from
Jun 21, 2025
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
28 changes: 28 additions & 0 deletions non-overlapping-intervals/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Arrays;

class Solution {
public int eraseOverlapIntervals(int[][] intervals) {

// 끝점 기준 정렬
Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[1], o2[1]));

int overlap = 0;

int end = intervals[0][1];

for (int i = 1; i < intervals.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요 :) 12주차까지 꾸준히 하는거 대단하시네요!! 앞으로 남은기간도 화이팅 💯

사소한 디테일이지만 0이 아니라 1부터 시작하는거 좋은 것 같아요 ㅎㅎ

int currentStart = intervals[i][0];
int currentEnd = intervals[i][1];

if (currentStart < end) {
overlap++;
} else {
end = currentEnd;
}

}

return overlap;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.*;

public class Solution {

// Union Find
public int countComponents(int n, int[][] edges) {

List<Integer> parent = clearGraph(n);

for (int[] edge : edges) {
union(parent, edge[0], edge[1]);
}

Set<Integer> componentRoots = new HashSet<>();
for (int i = 0; i < n; i++) {
componentRoots.add(find(parent, i));
}

return componentRoots.size();
}

private void union(List<Integer> parent, int x, int y) {
int px = find(parent, x);
int py = find(parent, y);
if (px > py) {
parent.set(py, px);
} else {
parent.set(px, py);
}
}

private int find(List<Integer> parent, int x) {
if (parent.get(x) == x) {
return x;
}
parent.set(x, find(parent, parent.get(x)));
return parent.get(x);
}

private List<Integer> clearGraph(int n) {
List<Integer> parent = new ArrayList<>();
for (int i = 0; i < n; i++) {
parent.add(i);
}
return parent;
}
}


39 changes: 39 additions & 0 deletions remove-nth-node-from-end-of-list/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {

// length 1인 경우를 위해 temp 생성
ListNode temp = new ListNode(0);
temp.next = head;

// 투 포인터 선언
ListNode fast = temp;
ListNode slow = temp;

// n + 1칸만큼 fast 먼저 이동
for (int i = 0; i < n + 1; i++) {
fast = fast.next;
}

while (fast != null) {
fast = fast.next;
// 끊어지는 노드 바로 앞까지 이동
slow = slow.next;
}

// slow.next = 끊어져서 치환해야 하는 위치
slow.next = slow.next.next;

return temp.next;
}
}

26 changes: 26 additions & 0 deletions same-tree/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {

// 현재 노드 검사 후 좌우 검사
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
if (p.val != q.val) return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
Copy link
Contributor

Choose a reason for hiding this comment

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

Queue 사용한 방식으로만 풀었는데 재귀를 사용하니까 가독성이 되게 좋네요 👍👍

}
}

60 changes: 60 additions & 0 deletions serialize-and-deserialize-binary-tree/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {

private StringBuilder sb = new StringBuilder();

// Encodes a tree to a single string.
public String serialize(TreeNode root) {
serializeNode(root);
return sb.toString();
}

private void serializeNode(TreeNode node) {
// null인 경우 구분 위해 null 문자열 삽입
if (node == null) {
sb.append("null,");
return;
}
sb.append(node.val).append(",");
serializeNode(node.left);
serializeNode(node.right);
}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] dataArr = data.split(",");
Queue<String> queue = new LinkedList<>(Arrays.asList(dataArr));
return buildTree(queue);
}

private TreeNode buildTree(Queue<String> queue) {
Copy link
Contributor

@minji-go minji-go Jun 19, 2025

Choose a reason for hiding this comment

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

저는 Queue 자체를 파라미터로 넘겨서 하는 방식이 생소해서 생각하지 못했는데 잘 구현하셨네요!

same-tree 문제도 그렇고 여기서도 dfs를 사용하시는게 익숙하신가보네요!
bfs로 풀어보시는 것도 좋을거 같아요 😄

String val = queue.poll();
if (val.equals("null")) {
return null;
}
TreeNode node = new TreeNode(Integer.parseInt(val));
node.left = buildTree(queue);
node.right = buildTree(queue);

return node;

}
}

// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));