Skip to content

Commit d198ece

Browse files
authored
Merge pull request #1585 from YoungSeok-Choi/feature/week-12
[YoungSeok-Choi] Week 12 solutions
2 parents 2c06b4d + 2ed7544 commit d198ece

File tree

5 files changed

+287
-0
lines changed

5 files changed

+287
-0
lines changed

missing-number/YoungSeok-Choi.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
class Solution {
3+
public int missingNumber(int[] nums) {
4+
5+
int len = nums.length;
6+
Map<Integer, Boolean> nMap = new HashMap<>();
7+
8+
for (int i = 0; i <= len; i++) {
9+
nMap.put(i, true);
10+
}
11+
12+
for (int anInt : nums) {
13+
nMap.remove(anInt);
14+
}
15+
16+
List<Integer> keyList = new ArrayList<>(nMap.keySet());
17+
18+
return keyList.get(0);
19+
}
20+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
// 문제 해결에 용이하게 주어진 단방향 그래프를 양방향으로 바꾸어 DFS 실행
7+
class Solution {
8+
9+
public int countComponents(int n, int[][] edges) {
10+
List<List<Integer>> graph = new ArrayList<>();
11+
boolean[] visited = new boolean[n];
12+
13+
// 인접 리스트 초기화
14+
for (int i = 0; i < n; i++) {
15+
graph.add(new ArrayList<>());
16+
}
17+
18+
// 양방향 그래프 구성
19+
for (int[] edge : edges) {
20+
graph.get(edge[0]).add(edge[1]);
21+
graph.get(edge[1]).add(edge[0]);
22+
}
23+
24+
int count = 0;
25+
26+
for (int i = 0; i < n; i++) {
27+
if (!visited[i]) {
28+
dfs(i, graph, visited);
29+
count++;
30+
}
31+
}
32+
33+
return count;
34+
}
35+
36+
private void dfs(int node, List<List<Integer>> graph, boolean[] visited) {
37+
visited[node] = true;
38+
for (int neighbor : graph.get(node)) {
39+
if (!visited[neighbor]) {
40+
dfs(neighbor, graph, visited);
41+
}
42+
}
43+
}
44+
}
45+
46+
// NOTE: 문제에서 주어진 무방향 그래프를 탐색하다, 앞 순번 DFS에서 연결 요소로 판단한 정점을 뒤 DFS 에서 다시 방문한 경우의
47+
// 처리가 까다로웠다
48+
class WrongSolution {
49+
50+
Map<Integer, Boolean> visitMap = new HashMap<>();
51+
boolean[] visit;
52+
public int cnt = 0;
53+
54+
public int countComponents(int n, int[][] edges) {
55+
56+
visit = new boolean[n];
57+
58+
for (int i = 0; i < n; i++) {
59+
if (!visit[i]) {
60+
visitMap = new HashMap<>();
61+
cnt++;
62+
dfs(i, edges);
63+
64+
}
65+
}
66+
67+
return cnt;
68+
}
69+
70+
public void dfs(int v, int[][] edges) {
71+
visit[v] = true;
72+
visitMap.put(v, true);
73+
74+
for (int i = 0; i < edges.length; i++) {
75+
if (edges[i][0] == v && !visit[edges[i][1]]) {
76+
dfs(edges[i][1], edges);
77+
} else if (edges[i][0] == v && visit[edges[i][1]]) {
78+
if (!visitMap.containsKey(edges[i][1])) {
79+
cnt--;
80+
}
81+
}
82+
}
83+
}
84+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class ListNode {
2+
int val;
3+
ListNode next;
4+
5+
ListNode() {
6+
}
7+
8+
ListNode(int val) {
9+
this.val = val;
10+
}
11+
12+
ListNode(int val, ListNode next) {
13+
this.val = val;
14+
this.next = next;
15+
}
16+
}
17+
18+
class Solution {
19+
public ListNode removeNthFromEnd(ListNode head, int n) {
20+
int length = 0;
21+
ListNode cur = head;
22+
23+
while (cur != null) {
24+
cur = cur.next;
25+
length++;
26+
}
27+
28+
int start = 1;
29+
int targetIdx = length - n + 1;
30+
ListNode result = new ListNode(-1);
31+
ListNode c = result;
32+
33+
while (head != null) {
34+
if (targetIdx == start) {
35+
head = head.next;
36+
start++;
37+
continue;
38+
}
39+
40+
c.next = new ListNode(head.val);
41+
c = c.next;
42+
start++;
43+
head = head.next;
44+
}
45+
46+
return result.next;
47+
}
48+
}

reorder-list/YoungSeok-Choi.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
public class ListNode {
2+
int val;
3+
ListNode next;
4+
5+
ListNode() {
6+
}
7+
8+
ListNode(int val) {
9+
this.val = val;
10+
}
11+
12+
ListNode(int val, ListNode next) {
13+
this.val = val;
14+
this.next = next;
15+
}
16+
}
17+
18+
class Solution {
19+
public void reorderList(ListNode head) {
20+
List<Integer> nArr = new ArrayList<>();
21+
Map<Integer, List<ListNode>> lMap = new HashMap<>();
22+
23+
ListNode cur = head.next;
24+
while (cur != null) {
25+
ListNode next = cur.next;
26+
cur.next = null;
27+
nArr.add(cur.val);
28+
29+
if (lMap.containsKey(cur.val)) {
30+
lMap.get(cur.val).add(cur);
31+
} else {
32+
List<ListNode> temp = new ArrayList<>();
33+
temp.add(cur);
34+
lMap.put(cur.val, temp);
35+
}
36+
37+
cur = next;
38+
}
39+
40+
int[] arr = nArr.stream().mapToInt(i -> i).toArray();
41+
int start = 0;
42+
int end = arr.length - 1;
43+
44+
while (start < end) {
45+
head.next = lMap.get(arr[end]).remove(0);
46+
head = head.next;
47+
head.next = lMap.get(arr[start]).remove(0);
48+
head = head.next;
49+
50+
start++;
51+
end--;
52+
}
53+
54+
if (arr.length % 2 != 0) {
55+
for (int anInt : new ArrayList<>(lMap.keySet())) {
56+
List<ListNode> anArr = lMap.get(anInt);
57+
58+
if (anArr.size() > 0) {
59+
head.next = anArr.remove(0);
60+
}
61+
}
62+
}
63+
}
64+
}
65+
66+
// NOTE: 동일한 val의 ListNode를 고려하지 못하여 틀린 경우
67+
class WrongSolution {
68+
public void reorderList(ListNode head) {
69+
List<Integer> nArr = new ArrayList<>();
70+
Map<Integer, ListNode> lMap = new HashMap<>();
71+
72+
ListNode cur = head.next;
73+
while (cur != null) {
74+
ListNode next = cur.next;
75+
cur.next = null;
76+
nArr.add(cur.val);
77+
lMap.put(cur.val, cur);
78+
cur = next;
79+
}
80+
81+
int[] arr = nArr.stream().mapToInt(i -> i).toArray();
82+
int start = 0;
83+
int end = arr.length - 1;
84+
85+
while (start < end) {
86+
head.next = lMap.remove(arr[end]);
87+
head = head.next;
88+
head.next = lMap.remove(arr[start]);
89+
head = head.next;
90+
91+
start++;
92+
end--;
93+
}
94+
95+
if (arr.length % 2 != 0) {
96+
for (int anInt : new ArrayList<>(lMap.keySet())) {
97+
head.next = lMap.remove(anInt);
98+
}
99+
}
100+
}
101+
}

same-tree/YoungSeok-Choi.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class TreeNode {
2+
int val;
3+
TreeNode left;
4+
TreeNode right;
5+
6+
TreeNode() {
7+
}
8+
9+
TreeNode(int val) {
10+
this.val = val;
11+
}
12+
13+
TreeNode(int val, TreeNode left, TreeNode right) {
14+
this.val = val;
15+
this.left = left;
16+
this.right = right;
17+
}
18+
}
19+
20+
class Solution {
21+
public boolean isSameTree(TreeNode p, TreeNode q) {
22+
23+
if (p == null && q == null) {
24+
return true;
25+
}
26+
27+
if (p == null || q == null || q.val != p.val) {
28+
return false;
29+
}
30+
31+
// 특정 깊이의 Node 가 같다면 null을 만날때 까지 탐색하고, 같지 않다면 바로 false를 반환.
32+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
33+
}
34+
}

0 commit comments

Comments
 (0)