Skip to content

Commit 16dddfa

Browse files
2 parents 37cada7 + 5e35d18 commit 16dddfa

File tree

198 files changed

+8459
-54
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

198 files changed

+8459
-54
lines changed

clone-graph/YoungSeok-Choi.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/*
5+
// Definition for a Node.
6+
class Node {
7+
public int val;
8+
public List<Node> neighbors;
9+
public Node() {
10+
val = 0;
11+
neighbors = new ArrayList<Node>();
12+
}
13+
public Node(int _val) {
14+
val = _val;
15+
neighbors = new ArrayList<Node>();
16+
}
17+
public Node(int _val, ArrayList<Node> _neighbors) {
18+
val = _val;
19+
neighbors = _neighbors;
20+
}
21+
}
22+
*/
23+
class Solution {
24+
public Map<Node, Node> nMap = new HashMap<>();
25+
26+
public Node cloneGraph(Node node) {
27+
if (node == null)
28+
return null;
29+
30+
if (nMap.containsKey(node)) {
31+
return nMap.get(node);
32+
}
33+
34+
Node clone = new Node(node.val);
35+
nMap.put(node, clone);
36+
37+
for (Node nei : node.neighbors) {
38+
clone.neighbors.add(cloneGraph(nei));
39+
}
40+
41+
return clone;
42+
}
43+
}
44+
45+
class WrongSolution {
46+
public Node graph;
47+
public Map<Node, Node> nMap = new HashMap<>();
48+
49+
public Node cloneGraph(Node node) {
50+
if (node == null)
51+
return null;
52+
53+
graph = new Node(node.val);
54+
nMap.put(node, graph);
55+
56+
// print(graph);
57+
58+
return clone(node, graph);
59+
}
60+
61+
public Node clone(Node node, Node cur) {
62+
for (int i = 0; i < node.neighbors.size(); i++) {
63+
Node adj = node.neighbors.get(i);
64+
65+
if (nMap.containsKey(adj)) {
66+
return nMap.get(adj);
67+
}
68+
69+
nMap.put(adj, new Node(adj.val));
70+
cur.neighbors.add(nMap.get(adj));
71+
clone(adj, nMap.get(adj));
72+
}
73+
74+
return cur;
75+
}
76+
77+
public void print(Node node) {
78+
System.out.println("visit " + node.val);
79+
80+
for (int i = 0; i < node.neighbors.size(); i++) {
81+
Node adj = node.neighbors.get(i);
82+
System.out.println("nei " + adj.val);
83+
print(adj);
84+
}
85+
}
86+
}

clone-graph/byol-han.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* https://leetcode.com/problems/clone-graph/
3+
* // Definition for a _Node.
4+
* function _Node(val, neighbors) {
5+
* this.val = val === undefined ? 0 : val;
6+
* this.neighbors = neighbors === undefined ? [] : neighbors;
7+
* };
8+
* 시간 복잡도: O(N) — 노드 수만큼 순회
9+
* 공간 복잡도: O(N) — visited 맵과 재귀 호출 스택
10+
*/
11+
12+
/**
13+
* @param {_Node} node
14+
* @return {_Node}
15+
*/
16+
var cloneGraph = function (node) {
17+
if (!node) return null;
18+
19+
const visited = new Map();
20+
21+
const dfs = (n) => {
22+
if (visited.has(n)) {
23+
return visited.get(n);
24+
}
25+
26+
const clone = new Node(n.val);
27+
visited.set(n, clone);
28+
29+
for (let neighbor of n.neighbors) {
30+
clone.neighbors.push(dfs(neighbor));
31+
}
32+
33+
return clone;
34+
};
35+
36+
return dfs(node);
37+
};

clone-graph/clara-shin.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* BFS를 사용한 그래프 복제
3+
* @param {_Node} node
4+
* @return {_Node}
5+
*/
6+
var cloneGraph = function (node) {
7+
if (!node) return null;
8+
9+
// 원본 노드와 복제된 노드를 매핑하는 해시맵
10+
const cloned = new Map();
11+
12+
// BFS를 위한 큐
13+
const queue = [node];
14+
15+
// 시작 노드 복제
16+
cloned.set(node, new _Node(node.val));
17+
18+
while (queue.length > 0) {
19+
const currentNode = queue.shift();
20+
21+
// 현재 노드의 모든 이웃들을 처리
22+
for (let neighbor of currentNode.neighbors) {
23+
// 이웃이 아직 복제되지 않았다면
24+
if (!cloned.has(neighbor)) {
25+
// 새로운 노드 생성하고 맵에 저장
26+
cloned.set(neighbor, new _Node(neighbor.val));
27+
// 큐에 추가하여 나중에 처리
28+
queue.push(neighbor);
29+
}
30+
31+
// 복제된 현재 노드의 이웃 리스트에 복제된 이웃 추가
32+
cloned.get(currentNode).neighbors.push(cloned.get(neighbor));
33+
}
34+
}
35+
36+
return cloned.get(node);
37+
};

clone-graph/krokerdile.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {_Node} node
3+
* @return {_Node}
4+
*/
5+
var cloneGraph = function(node) {
6+
if (!node) return null;
7+
8+
const visited = new Map(); // 원본 노드 -> 복제 노드
9+
10+
function dfs(curr) {
11+
if (visited.has(curr)) {
12+
return visited.get(curr); // 이미 복제한 경우
13+
}
14+
15+
const copy = new _Node(curr.val);
16+
visited.set(curr, copy); // 복제한 노드 저장
17+
18+
for (const neighbor of curr.neighbors) {
19+
copy.neighbors.push(dfs(neighbor)); // 이웃도 재귀적으로 복사
20+
}
21+
22+
return copy;
23+
}
24+
25+
return dfs(node);
26+
};
27+

clone-graph/lhc0506.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* // Definition for a _Node.
3+
* function _Node(val, neighbors) {
4+
* this.val = val === undefined ? 0 : val;
5+
* this.neighbors = neighbors === undefined ? [] : neighbors;
6+
* };
7+
*/
8+
9+
/**
10+
* @param {_Node} node
11+
* @return {_Node}
12+
*/
13+
var cloneGraph = function(node) {
14+
if (!node) return null;
15+
16+
const stack = [node];
17+
const map = new Map();
18+
19+
map.set(node.val, new _Node(node.val));
20+
21+
while (stack.length > 0) {
22+
const currentNode = stack.pop();
23+
24+
for (const neighbor of currentNode.neighbors) {
25+
if (!map.has(neighbor.val)) {
26+
map.set(neighbor.val, new Node(neighbor.val));
27+
stack.push(neighbor);
28+
}
29+
map.get(currentNode.val).neighbors.push(map.get(neighbor.val));
30+
}
31+
32+
}
33+
return map.get(node.val);
34+
};
35+
36+
37+
// 시간복잡도: O(n)
38+
// 공간복잡도: O(n)

clone-graph/moonjonghoo.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* // Definition for a Node.
3+
* function Node(val, neighbors) {
4+
* this.val = val === undefined ? 0 : val;
5+
* this.neighbors = neighbors === undefined ? [] : neighbors;
6+
* };
7+
*/
8+
9+
/**
10+
* @param {Node} node
11+
* @return {Node}
12+
*/
13+
var cloneGraph = function (node) {
14+
if (!node) return null;
15+
16+
const visited = new Map();
17+
18+
const dfs = (currNode) => {
19+
if (visited.has(currNode)) {
20+
return visited.get(currNode);
21+
}
22+
23+
// 노드 복사
24+
const clone = new Node(currNode.val);
25+
visited.set(currNode, clone);
26+
27+
// 이웃 노드들도 복사해서 연결
28+
for (let neighbor of currNode.neighbors) {
29+
clone.neighbors.push(dfs(neighbor));
30+
}
31+
32+
return clone;
33+
};
34+
35+
return dfs(node);
36+
};

clone-graph/sejineer.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
시간 복잡도: O(V + E) V: 노드 개수, E: 간선 개수
3+
공간 복잡도: O(V)
4+
"""
5+
from collections import deque
6+
from typing import Optional
7+
class Solution:
8+
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
9+
if not node:
10+
return
11+
12+
clone = Node(node.val)
13+
vis = {node: clone}
14+
queue = deque([node])
15+
16+
while queue:
17+
cur = queue.popleft()
18+
for nxt in cur.neighbors:
19+
if nxt not in vis:
20+
vis[nxt] = Node(nxt.val)
21+
queue.append(nxt)
22+
vis[cur].neighbors.append(vis[nxt])
23+
return clone

clone-graph/sora0319.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public List<Node> neighbors;
6+
public Node() {
7+
val = 0;
8+
neighbors = new ArrayList<Node>();
9+
}
10+
public Node(int _val) {
11+
val = _val;
12+
neighbors = new ArrayList<Node>();
13+
}
14+
public Node(int _val, ArrayList<Node> _neighbors) {
15+
val = _val;
16+
neighbors = _neighbors;
17+
}
18+
}
19+
*/
20+
21+
class Solution {
22+
Map<Node, Node> visited = new HashMap<>();
23+
24+
public Node cloneGraph(Node node) {
25+
if (node == null) return null;
26+
27+
if (visited.containsKey(node)) {
28+
return visited.get(node);
29+
}
30+
31+
Node clone = new Node(node.val);
32+
visited.put(node, clone);
33+
34+
for (Node neighbor : node.neighbors) {
35+
clone.neighbors.add(cloneGraph(neighbor));
36+
}
37+
38+
return clone;
39+
}
40+
}
41+

clone-graph/uraflower.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Definition for a _Node.
2+
function _Node(val, neighbors) {
3+
this.val = val === undefined ? 0 : val;
4+
this.neighbors = neighbors === undefined ? [] : neighbors;
5+
};
6+
7+
/**
8+
* 그래프를 깊은 복사하여 반환하는 함수
9+
* @param {_Node} node
10+
* @return {_Node}
11+
*/
12+
const cloneGraph = function (node) {
13+
if (!node) return null;
14+
15+
function dfs(node, visited) {
16+
const current = new _Node(node.val);
17+
visited.set(node, current);
18+
19+
node.neighbors.forEach((neighbor) => {
20+
const clonedNeighbor = visited.has(neighbor) ? visited.get(neighbor) : dfs(neighbor, visited);
21+
current.neighbors.push(clonedNeighbor);
22+
});
23+
24+
return current;
25+
}
26+
27+
return dfs(node, new Map()); // visited: 원본 노드를 key, 클론한 노드를 value로 하는 맵
28+
};
29+
30+
// 시간복잡도: O(V + E) (모든 노드와 간선을 한 번씩 순회)
31+
// 공간복잡도: O(V) (visited 맵 + 재귀 호출 스택)

0 commit comments

Comments
 (0)