Skip to content

Commit

Permalink
add 133 java, update review list
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 30, 2023
1 parent f745d3c commit 1fb7804
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@
126|[Word Ladder II](https://leetcode.com/problems/word-ladder-ii/)| [Python](./leetcode_python/Breadth-First-Search/word-ladder-ii.py) ||| Hard |complex, trick, dfs, bfs, dfs+bfs, check `# 127 Word Ladder`,`amazon` | AGAIN*** (3)
127| [Word Ladder](https://leetcode.com/problems/word-ladder/)| [Python](./leetcode_python/Breadth-First-Search/word-ladder.py) | _O(n * d)_ | _O(d)_ | Hard/Medium |good basic, check #126 Word Ladder II, `bfs`, `UBER`, `amazon`, `M$`, `fb`| AGAIN************** (9)
130| [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)| [Python](./leetcode_python/Breadth-First-Search/surrounded-regions.py) | _O(m + n)_ | | Medium |`bfs`, `dfs`,`union find`,good basic, `amazon`| AGAIN*********** (5)
133| [Clone Graph](https://leetcode.com/problems/clone-graph/)| [Python](./leetcode_python/Breadth-First-Search/clone-graph.py) | _O(n)_ | _O(n)_ | Medium |Curated Top 75, good trick, `check #138 Copy List with Random Pointer `,`graph`,`dfs`,`bfs`, copy, `UBER`, `google`,`amazon`,`fb`| AGAIN**************** (9) (MUST)
133| [Clone Graph](https://leetcode.com/problems/clone-graph/)| [Python](./leetcode_python/Breadth-First-Search/clone-graph.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Graph/CloneGraph.java) | _O(n)_ | _O(n)_ | Medium |Curated Top 75, good trick, `check #138 Copy List with Random Pointer `,`graph`,`dfs`,`bfs`, `UBER`, `google`,`amazon`,`fb`| AGAIN**************** (9) (MUST)
207| [Course Schedule](https://leetcode.com/problems/course-schedule/)| [Python](./leetcode_python/Breadth-First-Search/course-schedule.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/CourseSchedule.java) | _O(\|V\| + \|E\|)_ | _O(\|E\|)_ | Medium |Curated Top 75, Topological Sort, backtrack, `good trick`,`dfs`, `bfs` , `amazon`,`fb`| AGAIN**************** (12) (MUST)
210| [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)| [Python](./leetcode_python/Breadth-First-Search/course-schedule-ii.py) | _O(\|V\| + \|E\|)_ | _O(\|E\|)_ | Medium |Topological Sort,check `# 207 Course Schedule ` first, `dfs`, `bfs` ,`amazon` ,`fb` | AGAIN********* (9) (again)
261| [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)| [Python](./leetcode_python/Breadth-First-Search/graph-valid-tree.py) | _O(\|V\| + \|E\|)_ | _O(\|V\| + \|E\|)_ | Medium | Curated Top 75, AGAIN, bfs, dfs, grpah, 🔒, `graph`,`quick union`,`union find` ,`google`,`amazon`,`fb`| AGAIN************* (8)
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20230930: 133
20230903: 654,106,105
20230830: 200
20230827: 131,17
Expand Down
13 changes: 9 additions & 4 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
2023-11-24 -> ['133']
2023-11-03 -> ['133']
2023-10-28 -> ['654,106,105']
2023-10-24 -> ['200']
2023-10-21 -> ['131,17']
2023-10-21 -> ['133', '131,17']
2023-10-16 -> ['79']
2023-10-15 -> ['40']
2023-10-14 -> ['90']
2023-10-13 -> ['46']
2023-10-13 -> ['133', '46']
2023-10-08 -> ['133']
2023-10-07 -> ['654,106,105', '78,39']
2023-10-06 -> ['355']
2023-10-05 -> ['621']
2023-10-03 -> ['200', '973,215']
2023-10-05 -> ['133', '621']
2023-10-03 -> ['133', '200', '973,215']
2023-10-02 -> ['133']
2023-10-01 -> ['133']
2023-09-30 -> ['131,17']
2023-09-25 -> ['79']
2023-09-24 -> ['654,106,105', '40']
Expand Down
160 changes: 119 additions & 41 deletions leetcode_java/src/main/java/LeetCodeJava/Graph/CloneGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
import java.util.*;

// https://leetcode.com/problems/clone-graph/
// NOTE !!! : Node.val is unique for each node.
/**
* 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.
*/


/*
// Definition for a Node.
Expand All @@ -28,60 +39,127 @@ public Node(int _val, ArrayList<Node> _neighbors) {

public class CloneGraph {

// TODO : fix below
// V0
Node clonedNode = new Node();
public Node cloneGraph(Node node) {

if (node == null){
// Node clonedNode = new Node();
// public Node cloneGraph(Node node) {
//
// if (node == null){
// return node;
// }
//
// // build map : {node_val : node_neighbors}
// Map<Integer, Set<Node>> map = new HashMap<>();
// for (Node x : node.neighbors){
// int _val = x.val;
// List<Node> _neighbors = x.neighbors;
// if (!map.containsKey(_val)){
// map.put(_val, new HashSet<>());
// }
// for (Node y : _neighbors){
// map.get(_val).add(y);
// }
// }
//
// List<Integer> visited = new ArrayList<>();
// // (status) 0 : not visited, 1 : visiting, 2 : visited
// int status = 0;
// _help(node, visited, map, status);
// return this.clonedNode;
// }
//
// private void _help(Node node, List<Integer> visited, Map<Integer, Set<Node>> map, int status){
//
// // all nodes are visited
// if (visited.size() == map.keySet().size()){
// return;
// }
//
// if (!visited.contains(node)){
// this.clonedNode = node;
// visited.add(node.val);
// if (map.get(node).isEmpty()){
// status = 2;
// map.remove(node.val);
// }
// }
//
// for (Node _node : map.get(node)){
// // remove visiting node in map val
// map.get(_node.val).remove(_node);
// _help(_node, visited, map, 1);
// }
//
// }

// V1
// IDEA : DFS
// https://leetcode.com/problems/clone-graph/editorial/
private HashMap <Node, Node> visited = new HashMap <> ();
public Node cloneGraph_1(Node node) {
if (node == null) {
return node;
}

// build map : {node_val : node_neighbors}
Map<Integer, Set<Node>> map = new HashMap<>();
for (Node x : node.neighbors){
int _val = x.val;
List<Node> _neighbors = x.neighbors;
if (!map.containsKey(_val)){
map.put(_val, new HashSet<>());
}
for (Node y : _neighbors){
map.get(_val).add(y);
}
// If the node was already visited before.
// Return the clone from the visited dictionary.
if (visited.containsKey(node)) {
return visited.get(node);
}

List<Integer> visited = new ArrayList<>();
// (status) 0 : not visited, 1 : visiting, 2 : visited
int status = 0;
_help(node, visited, map, status);
return this.clonedNode;
}

private void _help(Node node, List<Integer> visited, Map<Integer, Set<Node>> map, int status){
// Create a clone for the given node.
// Note that we don't have cloned neighbors as of now, hence [].
Node cloneNode = new Node(node.val, new ArrayList());
// The key is original node and value being the clone node.
visited.put(node, cloneNode);

// all nodes are visited
if (visited.size() == map.keySet().size()){
return;
// Iterate through the neighbors to generate their clones
// and prepare a list of cloned neighbors to be added to the cloned node.
for (Node neighbor: node.neighbors) {
cloneNode.neighbors.add(cloneGraph_1(neighbor));
}

if (!visited.contains(node)){
this.clonedNode = node;
visited.add(node.val);
if (map.get(node).isEmpty()){
status = 2;
map.remove(node.val);
}
}
// NOTE !!! after dfs, we return final result
return cloneNode;
}

for (Node _node : map.get(node)){
// remove visiting node in map val
map.get(_node.val).remove(_node);
_help(_node, visited, map, 1);
// V2
// IDEA : BFS
// https://leetcode.com/problems/clone-graph/editorial/
public Node cloneGraph_2(Node node) {
if (node == null) {
return node;
}

}
// Hash map to save the visited node and it's respective clone
// as key and value respectively. This helps to avoid cycles.
HashMap<Node, Node> visited = new HashMap();

// Put the first node in the queue
LinkedList<Node> queue = new LinkedList<Node> ();
queue.add(node);
// Clone the node and put it in the visited dictionary.
visited.put(node, new Node(node.val, new ArrayList()));

// Start BFS traversal
while (!queue.isEmpty()) {
// Pop a node say "n" from the front of the queue.
Node n = queue.remove();
// Iterate through all the neighbors of the node "n"
for (Node neighbor: n.neighbors) {
if (!visited.containsKey(neighbor)) {
// Clone the neighbor and put in the visited, if not present already
visited.put(neighbor, new Node(neighbor.val, new ArrayList()));
// Add the newly encountered node to the queue.
queue.add(neighbor);
}
// Add the clone of the neighbor to the neighbors of the clone node "n".
visited.get(n).neighbors.add(visited.get(neighbor));
}
}

public static void main(String[] args) {
System.out.println(new Node());
// Return the clone of the node from visited.
return visited.get(node);
}

}

0 comments on commit 1fb7804

Please sign in to comment.