diff --git a/non-overlapping-intervals/Tessa1217.java b/non-overlapping-intervals/Tessa1217.java new file mode 100644 index 000000000..6bbdbb92d --- /dev/null +++ b/non-overlapping-intervals/Tessa1217.java @@ -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++) { + int currentStart = intervals[i][0]; + int currentEnd = intervals[i][1]; + + if (currentStart < end) { + overlap++; + } else { + end = currentEnd; + } + + } + + return overlap; + } +} + diff --git a/number-of-connected-components-in-an-undirected-graph/Tessa1217.java b/number-of-connected-components-in-an-undirected-graph/Tessa1217.java new file mode 100644 index 000000000..b6c80620f --- /dev/null +++ b/number-of-connected-components-in-an-undirected-graph/Tessa1217.java @@ -0,0 +1,49 @@ +import java.util.*; + +public class Solution { + + // Union Find + public int countComponents(int n, int[][] edges) { + + List parent = clearGraph(n); + + for (int[] edge : edges) { + union(parent, edge[0], edge[1]); + } + + Set componentRoots = new HashSet<>(); + for (int i = 0; i < n; i++) { + componentRoots.add(find(parent, i)); + } + + return componentRoots.size(); + } + + private void union(List 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 parent, int x) { + if (parent.get(x) == x) { + return x; + } + parent.set(x, find(parent, parent.get(x))); + return parent.get(x); + } + + private List clearGraph(int n) { + List parent = new ArrayList<>(); + for (int i = 0; i < n; i++) { + parent.add(i); + } + return parent; + } +} + + diff --git a/remove-nth-node-from-end-of-list/Tessa1217.java b/remove-nth-node-from-end-of-list/Tessa1217.java new file mode 100644 index 000000000..7baec6a9c --- /dev/null +++ b/remove-nth-node-from-end-of-list/Tessa1217.java @@ -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; + } +} + diff --git a/same-tree/Tessa1217.java b/same-tree/Tessa1217.java new file mode 100644 index 000000000..feca807fb --- /dev/null +++ b/same-tree/Tessa1217.java @@ -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); + } +} + diff --git a/serialize-and-deserialize-binary-tree/Tessa1217.java b/serialize-and-deserialize-binary-tree/Tessa1217.java new file mode 100644 index 000000000..4a63bca73 --- /dev/null +++ b/serialize-and-deserialize-binary-tree/Tessa1217.java @@ -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 queue = new LinkedList<>(Arrays.asList(dataArr)); + return buildTree(queue); + } + + private TreeNode buildTree(Queue queue) { + 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)); +