Skip to content

[YoungSeok-Choi] Week 12 solutions #1585

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 3 commits into from
Jun 23, 2025
Merged
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
20 changes: 20 additions & 0 deletions missing-number/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

class Solution {
public int missingNumber(int[] nums) {

int len = nums.length;
Map<Integer, Boolean> nMap = new HashMap<>();

for (int i = 0; i <= len; i++) {
nMap.put(i, true);
}

for (int anInt : nums) {
nMap.remove(anInt);
}

List<Integer> keyList = new ArrayList<>(nMap.keySet());

return keyList.get(0);
}
}
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> 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<List<Integer>> 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<Integer, Boolean> 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--;
}
}
}
}
}
48 changes: 48 additions & 0 deletions remove-nth-node-from-end-of-list/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
101 changes: 101 additions & 0 deletions reorder-list/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -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<Integer> nArr = new ArrayList<>();
Map<Integer, List<ListNode>> 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<ListNode> 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<ListNode> anArr = lMap.get(anInt);

if (anArr.size() > 0) {
head.next = anArr.remove(0);
}
}
}
}
}

// NOTE: 동일한 val의 ListNode를 고려하지 못하여 틀린 경우
class WrongSolution {
public void reorderList(ListNode head) {
List<Integer> nArr = new ArrayList<>();
Map<Integer, ListNode> 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);
}
}
}
}
34 changes: 34 additions & 0 deletions same-tree/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -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);
}
}