Skip to content

Commit ccfea3c

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 22e4e05 + b144d5d commit ccfea3c

File tree

126 files changed

+4575
-0
lines changed

Some content is hidden

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

126 files changed

+4575
-0
lines changed

β€Žclone-graph/HoonDongKang.tsβ€Ž

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* [Problem]: [133] Clone Graph
3+
* (https://leetcode.com/problems/longest-repeating-character-replacement/description/)
4+
*/
5+
6+
// Definition for _Node.
7+
class _Node {
8+
val: number;
9+
neighbors: _Node[];
10+
11+
constructor(val?: number, neighbors?: _Node[]) {
12+
this.val = val === undefined ? 0 : val;
13+
this.neighbors = neighbors === undefined ? [] : neighbors;
14+
}
15+
}
16+
17+
function cloneGraph(node: _Node | null): _Node | null {
18+
//μ‹œκ°„λ³΅μž‘λ„ O(n)
19+
//κ³΅κ°„λ³΅μž‘λ„ O(n)
20+
function dfsFunc(node: _Node | null): _Node | null {
21+
if (!node) return null;
22+
const map = new Map<_Node, _Node>();
23+
24+
function dfs(node: _Node): _Node {
25+
if (map.has(node)) return map.get(node)!;
26+
27+
const copy = new _Node(node.val);
28+
map.set(node, copy);
29+
30+
for (const neighbor of node.neighbors) {
31+
copy.neighbors.push(dfs(neighbor));
32+
}
33+
34+
return copy;
35+
}
36+
37+
return dfs(node);
38+
}
39+
//μ‹œκ°„λ³΅μž‘λ„ O(n)
40+
//κ³΅κ°„λ³΅μž‘λ„ O(n)
41+
function bfsFunc(node: _Node | null): _Node | null {
42+
if (!node) return null;
43+
44+
const map = new Map<_Node, _Node>();
45+
const queue: _Node[] = [];
46+
47+
const clone = new _Node(node.val);
48+
map.set(node, clone);
49+
queue.push(node);
50+
51+
while (queue.length > 0) {
52+
const cur = queue.shift()!;
53+
54+
for (const neighbor of cur.neighbors) {
55+
if (!map.has(neighbor)) {
56+
map.set(neighbor, new _Node(neighbor.val));
57+
queue.push(neighbor);
58+
}
59+
60+
map.get(cur)!.neighbors.push(map.get(neighbor)!);
61+
}
62+
}
63+
return clone;
64+
}
65+
}

β€Žclone-graph/Jeehay28.tsβ€Ž

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class _Node {
2+
val: number;
3+
neighbors: _Node[];
4+
5+
constructor(val?: number, neighbors?: _Node[]) {
6+
this.val = val === undefined ? 0 : val;
7+
this.neighbors = neighbors === undefined ? [] : neighbors;
8+
}
9+
}
10+
11+
// TC: O(V + E), where V is the number of vertices and E is the number of edges
12+
// SC: O(V + E)
13+
function cloneGraph(node: _Node | null): _Node | null {
14+
// 1: [2, 4]
15+
// 2: [1, 3]
16+
// 3: [2, 4]
17+
// 4: [1, 3]
18+
19+
const clones = new Map<_Node, _Node>();
20+
// original Node: cloned Node
21+
22+
if (!node) return null;
23+
24+
const dfs = (node: _Node) => {
25+
if (clones.has(node)) {
26+
return clones.get(node);
27+
}
28+
29+
const clone = new _Node(node.val);
30+
clones.set(node, clone);
31+
32+
for (const nei of node.neighbors) {
33+
clone.neighbors.push(dfs(nei)!);
34+
}
35+
36+
return clone;
37+
};
38+
39+
return dfs(node)!;
40+
}
41+
42+
43+
// TC: O(V + E)
44+
// SC: O(V + E)
45+
// function cloneGraph(node: _Node | null): _Node | null {
46+
// if (!node) return null;
47+
48+
// const clone: _Node = new _Node(node.val);
49+
// const clones = new Map<_Node, _Node>();
50+
// clones.set(node, clone);
51+
// const queue: _Node[] = [node]; // BFS -> use queue
52+
53+
// while (queue.length > 0) {
54+
// const node = queue.shift()!;
55+
// for (const nei of node.neighbors) {
56+
// if (!clones.has(nei)) {
57+
// clones.set(nei, new _Node(nei.val));
58+
// queue.push(nei);
59+
// }
60+
// clones.get(node)!.neighbors.push(clones.get(nei)!);
61+
// }
62+
// }
63+
64+
// return clone;
65+
// }
66+
67+

β€Žclone-graph/PDKhan.cppβ€Ž

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
void dfs(Node* node, unordered_map<Node*, Node*>& map){
4+
if(map.count(node))
5+
return;
6+
7+
map[node] = new Node(node->val);
8+
9+
for(int i = 0; i < node->neighbors.size(); i++)
10+
dfs(node->neighbors[i], map);
11+
}
12+
13+
Node* cloneGraph(Node* node) {
14+
if(node == NULL)
15+
return NULL;
16+
17+
unordered_map<Node*, Node*> map;
18+
19+
dfs(node, map);
20+
21+
for(auto& x : map){
22+
Node* org = x.first;
23+
Node* dst = x.second;
24+
for(int i = 0; i < org->neighbors.size(); i++){
25+
dst->neighbors.push_back(map[org->neighbors[i]]);
26+
}
27+
}
28+
29+
return map[node];
30+
}
31+
};

β€Žclone-graph/Tessa1217.javaβ€Ž

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
/**
25+
* μ°Έμ‘° λ…Έλ“œλŠ” 무방ν–₯ κ·Έλž˜ν”„μ— μ—°κ²°λ˜μ–΄μžˆλ‹€. κ·Έλž˜ν”„μ˜ deep copy(clone)을 λ°˜ν™˜ν•˜μ„Έμš”.
26+
*/
27+
class Solution {
28+
29+
// λ°©λ¬Έν•œ λ…Έλ“œλ₯Ό κΈ°μ–΅ν•  Map μ„ μ–Έ
30+
Map<Node, Node> visited = new HashMap<>();
31+
32+
public Node cloneGraph(Node node) {
33+
return clone(node);
34+
}
35+
36+
public Node clone(Node node) {
37+
if (node == null) {
38+
return null;
39+
}
40+
41+
// 이미 λ°©λ¬Έν–ˆμœΌλ©΄ Mapμ—μ„œ κΊΌλ‚΄μ„œ λ°˜ν™˜
42+
if (visited.containsKey(node)) {
43+
return visited.get(node);
44+
}
45+
46+
// μ‹ κ·œ Node 생성
47+
Node newNode = new Node(node.val);
48+
visited.put(node, newNode);
49+
50+
// 인접 λ…Έλ“œ Clone
51+
if (node.neighbors != null && !node.neighbors.isEmpty()) {
52+
for (Node neighbor : node.neighbors) {
53+
newNode.neighbors.add(clone(neighbor));
54+
}
55+
}
56+
return newNode;
57+
}
58+
}
59+

β€Žclone-graph/ayosecu.pyβ€Ž

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from typing import Optional
2+
from collections import deque
3+
4+
# Definition for a Node.
5+
class Node:
6+
def __init__(self, val = 0, neighbors = None):
7+
self.val = val
8+
self.neighbors = neighbors if neighbors is not None else []
9+
10+
class Solution:
11+
"""
12+
- Time Complexity: O(N + E)
13+
- N = The number of nodes
14+
- E = The number of neighbors
15+
- Space Complexity: O(N + E)
16+
"""
17+
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
18+
if not node:
19+
return None
20+
21+
dq = deque([node])
22+
dic = {}
23+
dic[node] = Node(node.val)
24+
25+
while dq:
26+
pop_node = dq.popleft()
27+
28+
for n in pop_node.neighbors:
29+
if n not in dic:
30+
dq.append(n)
31+
dic[n] = Node(n.val)
32+
dic[pop_node].neighbors.append(dic[n])
33+
34+
return dic[node]
35+
36+
37+
### TEST CASES ###
38+
def build_graph(adj_list):
39+
if not adj_list:
40+
return None
41+
42+
nodes = {}
43+
for i in range(1, len(adj_list) + 1):
44+
nodes[i] = Node(i)
45+
46+
for i, neighbors in enumerate(adj_list, 1):
47+
nodes[i].neighbors = [nodes[n] for n in neighbors]
48+
49+
return nodes[1]
50+
51+
52+
def print_graph(node):
53+
if not node:
54+
print("None")
55+
return
56+
57+
visited = set()
58+
q = deque([node])
59+
while q:
60+
curr = q.popleft()
61+
if curr in visited:
62+
continue
63+
visited.add(curr)
64+
print(f"Node {curr.val}: {[n.val for n in curr.neighbors]}")
65+
for neighbor in curr.neighbors:
66+
if neighbor not in visited:
67+
q.append(neighbor)
68+
69+
tc = [
70+
[[2,4],[1,3],[2,4],[1,3]],
71+
[[]],
72+
[]
73+
]
74+
75+
sol = Solution()
76+
for i, adj_list in enumerate(tc, 1):
77+
original = build_graph(adj_list)
78+
print(f"===== TC {i} =====")
79+
print("Original Graph:")
80+
print_graph(original)
81+
82+
cloned = sol.cloneGraph(original)
83+
84+
print("\nCloned Graph:")
85+
print_graph(cloned)

β€Ž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+
};

0 commit comments

Comments
Β (0)