Skip to content

Commit 3e5e48c

Browse files
Problem 133 (#27)
* add solution and resources * add readme
1 parent 96bba74 commit 3e5e48c

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
4040
29. [Longest Substring Without Repeating Characters #3](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/longest-substring-3.md)
4141
30. [3Sum #15](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/3Sum-15.md)
4242
31. [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)
43-
32. Clone Graph #133
43+
32. [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
4444
33. Evaluate Reverse Polish Notation #150
4545
34. Course Schedule #207
4646
35. Implement Trie (Prefix Tree) #208
@@ -143,6 +143,7 @@ In order to practice with similar data structures I'll be placing each problem i
143143
- [Contains Duplicate #217](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/contains-duplicate-217.md)
144144
- [Longest Substring Without Repeating Characters #3](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/longest-substring-3.md)
145145
- [3Sum #15](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/3Sum-15.md)
146+
- [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
146147

147148
### Binary Tree
148149

@@ -153,6 +154,10 @@ In order to practice with similar data structures I'll be placing each problem i
153154
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
154155
- [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)
155156

157+
### Graph
158+
159+
- [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
160+
156161
### Heap
157162

158163
- [K Closest Points to Origin #973](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/k-closest-origin-973.md)
@@ -186,6 +191,7 @@ Within the problems above there are several patterns that often occur. I plan to
186191
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
187192
- [01 Matrix #542](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/01-matrix-542.md)
188193
- [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)
194+
- [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
189195

190196
### Depth First Search
191197

@@ -195,6 +201,7 @@ Within the problems above there are several patterns that often occur. I plan to
195201
- [Balanced Binary Tree #110](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/balanced-binary-tree-110.md)
196202
- [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
197203
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
204+
- [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
198205

199206
### Divide & Conquer
200207

medium/clone-graph-133.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Clone Graph
2+
3+
Page on leetcode: https://leetcode.com/problems/clone-graph/
4+
5+
## Problem Statement
6+
7+
Given a reference of a node in a connected undirected graph.
8+
9+
Return a deep copy (clone) of the graph.
10+
11+
Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.
12+
13+
```
14+
class Node {
15+
public int val;
16+
public List<Node> neighbors;
17+
}
18+
```
19+
20+
**Test case format:**
21+
22+
For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on. The graph is represented in the test case using an adjacency list.
23+
24+
An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
25+
26+
The given node will always be the first node with val = 1. You must return the copy of the given node as a reference to the cloned graph.
27+
28+
### Constraints
29+
30+
- The number of nodes in the graph is in the range [0, 100].
31+
- 1 <= Node.val <= 100
32+
- Node.val is unique for each node.
33+
- There are no repeated edges and no self-loops in the graph.
34+
- The Graph is connected and all nodes can be visited starting from the given node.
35+
36+
### Example
37+
38+
```
39+
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
40+
Output: [[2,4],[1,3],[2,4],[1,3]]
41+
Explanation: There are 4 nodes in the graph.
42+
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
43+
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
44+
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
45+
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
46+
```
47+
48+
## Solution
49+
50+
- Traverse all nodes
51+
- Make copies of nodes (use spread syntax?)
52+
- Return new node 1
53+
- No nodes then return node?
54+
- Single node still need to make a copy
55+
- BFS with queue, will it get stuck in a loop?
56+
57+
### Initial Pseudocode
58+
59+
1. Make empty queue
60+
2. Create copy of first node
61+
3. Add copy to queue
62+
4. While queue isn't empty
63+
5. Shift from queue
64+
6. Create copy of node and add to stack
65+
66+
### Optimized Solution
67+
68+
The below is a recursive "DFS" approach to solving the problem with a time and space complexity of O(n). You can see an explanation of this approach here: https://www.youtube.com/watch?v=mQeF6bN8hMk A discussion about using DFS and BFS can be viewed here: https://leetcode.com/problems/clone-graph/discuss/1793212/C%2B%2Bor-Detailed-Explanation-w-DFS-and-BFS-or-Commented-code-with-extra-Test-Case
69+
70+
```javascript
71+
const cloneGraph = function (node) {
72+
// Empty map for tracking which nodes are copied
73+
const map = {};
74+
75+
function dfs(node) {
76+
// Null case, covers the no-node case in the constraints
77+
if (!node) {
78+
return node;
79+
}
80+
// If the node does exist in the map, skip this block and return the already copied node
81+
if (!map[node.val]) {
82+
// If it's not in the map then make a new deep copy of the node
83+
map[node.val] = new Node(node.val);
84+
// Make copies of each of the neighbor nodes (recursively)
85+
map[node.val].neighbors = node.neighbors.map((n) => dfs(n));
86+
}
87+
// After neighbors are processed on the node return it
88+
return map[node.val];
89+
}
90+
91+
return dfs(node);
92+
};
93+
```

0 commit comments

Comments
 (0)