diff --git a/missing-number/YoungSeok-Choi.java b/missing-number/YoungSeok-Choi.java new file mode 100644 index 000000000..21ed3d39f --- /dev/null +++ b/missing-number/YoungSeok-Choi.java @@ -0,0 +1,20 @@ + +class Solution { + public int missingNumber(int[] nums) { + + int len = nums.length; + Map nMap = new HashMap<>(); + + for (int i = 0; i <= len; i++) { + nMap.put(i, true); + } + + for (int anInt : nums) { + nMap.remove(anInt); + } + + List keyList = new ArrayList<>(nMap.keySet()); + + return keyList.get(0); + } +} diff --git a/number-of-connected-components-in-an-undirected-graph/YoungSeok-Choi.java b/number-of-connected-components-in-an-undirected-graph/YoungSeok-Choi.java new file mode 100644 index 000000000..eed208df7 --- /dev/null +++ b/number-of-connected-components-in-an-undirected-graph/YoungSeok-Choi.java @@ -0,0 +1,84 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +// 문제 해결에 용이하게 주어진 단방향 그래프를 양방향으로 바꾸어 DFS 실행 +class Solution { + + public int countComponents(int n, int[][] edges) { + List> graph = new ArrayList<>(); + boolean[] visited = new boolean[n]; + + // 인접 리스트 초기화 + for (int i = 0; i < n; i++) { + graph.add(new ArrayList<>()); + } + + // 양방향 그래프 구성 + for (int[] edge : edges) { + graph.get(edge[0]).add(edge[1]); + graph.get(edge[1]).add(edge[0]); + } + + int count = 0; + + for (int i = 0; i < n; i++) { + if (!visited[i]) { + dfs(i, graph, visited); + count++; + } + } + + return count; + } + + private void dfs(int node, List> graph, boolean[] visited) { + visited[node] = true; + for (int neighbor : graph.get(node)) { + if (!visited[neighbor]) { + dfs(neighbor, graph, visited); + } + } + } +} + +// NOTE: 문제에서 주어진 무방향 그래프를 탐색하다, 앞 순번 DFS에서 연결 요소로 판단한 정점을 뒤 DFS 에서 다시 방문한 경우의 +// 처리가 까다로웠다 +class WrongSolution { + + Map visitMap = new HashMap<>(); + boolean[] visit; + public int cnt = 0; + + public int countComponents(int n, int[][] edges) { + + visit = new boolean[n]; + + for (int i = 0; i < n; i++) { + if (!visit[i]) { + visitMap = new HashMap<>(); + cnt++; + dfs(i, edges); + + } + } + + return cnt; + } + + public void dfs(int v, int[][] edges) { + visit[v] = true; + visitMap.put(v, true); + + for (int i = 0; i < edges.length; i++) { + if (edges[i][0] == v && !visit[edges[i][1]]) { + dfs(edges[i][1], edges); + } else if (edges[i][0] == v && visit[edges[i][1]]) { + if (!visitMap.containsKey(edges[i][1])) { + cnt--; + } + } + } + } +} diff --git a/remove-nth-node-from-end-of-list/YoungSeok-Choi.java b/remove-nth-node-from-end-of-list/YoungSeok-Choi.java new file mode 100644 index 000000000..0f2d67d17 --- /dev/null +++ b/remove-nth-node-from-end-of-list/YoungSeok-Choi.java @@ -0,0 +1,48 @@ +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) { + int length = 0; + ListNode cur = head; + + while (cur != null) { + cur = cur.next; + length++; + } + + int start = 1; + int targetIdx = length - n + 1; + ListNode result = new ListNode(-1); + ListNode c = result; + + while (head != null) { + if (targetIdx == start) { + head = head.next; + start++; + continue; + } + + c.next = new ListNode(head.val); + c = c.next; + start++; + head = head.next; + } + + return result.next; + } +} diff --git a/reorder-list/YoungSeok-Choi.java b/reorder-list/YoungSeok-Choi.java new file mode 100644 index 000000000..c505d5652 --- /dev/null +++ b/reorder-list/YoungSeok-Choi.java @@ -0,0 +1,101 @@ +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 void reorderList(ListNode head) { + List nArr = new ArrayList<>(); + Map> lMap = new HashMap<>(); + + ListNode cur = head.next; + while (cur != null) { + ListNode next = cur.next; + cur.next = null; + nArr.add(cur.val); + + if (lMap.containsKey(cur.val)) { + lMap.get(cur.val).add(cur); + } else { + List temp = new ArrayList<>(); + temp.add(cur); + lMap.put(cur.val, temp); + } + + cur = next; + } + + int[] arr = nArr.stream().mapToInt(i -> i).toArray(); + int start = 0; + int end = arr.length - 1; + + while (start < end) { + head.next = lMap.get(arr[end]).remove(0); + head = head.next; + head.next = lMap.get(arr[start]).remove(0); + head = head.next; + + start++; + end--; + } + + if (arr.length % 2 != 0) { + for (int anInt : new ArrayList<>(lMap.keySet())) { + List anArr = lMap.get(anInt); + + if (anArr.size() > 0) { + head.next = anArr.remove(0); + } + } + } + } +} + +// NOTE: 동일한 val의 ListNode를 고려하지 못하여 틀린 경우 +class WrongSolution { + public void reorderList(ListNode head) { + List nArr = new ArrayList<>(); + Map lMap = new HashMap<>(); + + ListNode cur = head.next; + while (cur != null) { + ListNode next = cur.next; + cur.next = null; + nArr.add(cur.val); + lMap.put(cur.val, cur); + cur = next; + } + + int[] arr = nArr.stream().mapToInt(i -> i).toArray(); + int start = 0; + int end = arr.length - 1; + + while (start < end) { + head.next = lMap.remove(arr[end]); + head = head.next; + head.next = lMap.remove(arr[start]); + head = head.next; + + start++; + end--; + } + + if (arr.length % 2 != 0) { + for (int anInt : new ArrayList<>(lMap.keySet())) { + head.next = lMap.remove(anInt); + } + } + } +} diff --git a/same-tree/YoungSeok-Choi.java b/same-tree/YoungSeok-Choi.java new file mode 100644 index 000000000..d110aaf4d --- /dev/null +++ b/same-tree/YoungSeok-Choi.java @@ -0,0 +1,34 @@ +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 || q.val != p.val) { + return false; + } + + // 특정 깊이의 Node 가 같다면 null을 만날때 까지 탐색하고, 같지 않다면 바로 false를 반환. + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); + } +}