Skip to content

Problem 133 #27

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 2 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
29. [Longest Substring Without Repeating Characters #3](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/longest-substring-3.md)
30. [3Sum #15](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/3Sum-15.md)
31. [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)
32. Clone Graph #133
32. [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
33. Evaluate Reverse Polish Notation #150
34. Course Schedule #207
35. Implement Trie (Prefix Tree) #208
Expand Down Expand Up @@ -143,6 +143,7 @@ In order to practice with similar data structures I'll be placing each problem i
- [Contains Duplicate #217](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/contains-duplicate-217.md)
- [Longest Substring Without Repeating Characters #3](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/longest-substring-3.md)
- [3Sum #15](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/3Sum-15.md)
- [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)

### Binary Tree

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

### Graph

- [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)

### Heap

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

### Depth First Search

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

### Divide & Conquer

Expand Down
93 changes: 93 additions & 0 deletions medium/clone-graph-133.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Clone Graph

Page on leetcode: https://leetcode.com/problems/clone-graph/

## Problem Statement

Given a reference of a node in a connected undirected graph.

Return a deep copy (clone) of the graph.

Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.

```
class Node {
public int val;
public List<Node> neighbors;
}
```

**Test case format:**

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.

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.

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.

### Constraints

- The number of nodes in the graph is in the range [0, 100].
- 1 <= Node.val <= 100
- Node.val is unique for each node.
- There are no repeated edges and no self-loops in the graph.
- The Graph is connected and all nodes can be visited starting from the given node.

### Example

```
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
Output: [[2,4],[1,3],[2,4],[1,3]]
Explanation: There are 4 nodes in the graph.
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
```

## Solution

- Traverse all nodes
- Make copies of nodes (use spread syntax?)
- Return new node 1
- No nodes then return node?
- Single node still need to make a copy
- BFS with queue, will it get stuck in a loop?

### Initial Pseudocode

1. Make empty queue
2. Create copy of first node
3. Add copy to queue
4. While queue isn't empty
5. Shift from queue
6. Create copy of node and add to stack

### Optimized Solution

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

```javascript
const cloneGraph = function (node) {
// Empty map for tracking which nodes are copied
const map = {};

function dfs(node) {
// Null case, covers the no-node case in the constraints
if (!node) {
return node;
}
// If the node does exist in the map, skip this block and return the already copied node
if (!map[node.val]) {
// If it's not in the map then make a new deep copy of the node
map[node.val] = new Node(node.val);
// Make copies of each of the neighbor nodes (recursively)
map[node.val].neighbors = node.neighbors.map((n) => dfs(n));
}
// After neighbors are processed on the node return it
return map[node.val];
}

return dfs(node);
};
```