From 1fb7804bbeef12ee8379460fe92b83429a8c5aab Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sat, 30 Sep 2023 19:45:07 +0800 Subject: [PATCH] add 133 java, update review list --- README.md | 2 +- data/progress.txt | 1 + data/to_review.txt | 13 +- .../java/LeetCodeJava/Graph/CloneGraph.java | 160 +++++++++++++----- 4 files changed, 130 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 1dff9b96..4e28b6d6 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/data/progress.txt b/data/progress.txt index cd669618..e06d11d6 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20230930: 133 20230903: 654,106,105 20230830: 200 20230827: 131,17 diff --git a/data/to_review.txt b/data/to_review.txt index eef438cd..8eadcbb2 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Graph/CloneGraph.java b/leetcode_java/src/main/java/LeetCodeJava/Graph/CloneGraph.java index 57b5be4d..a63b8ff7 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Graph/CloneGraph.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Graph/CloneGraph.java @@ -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. @@ -28,60 +39,127 @@ public Node(int _val, ArrayList _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> map = new HashMap<>(); +// for (Node x : node.neighbors){ +// int _val = x.val; +// List _neighbors = x.neighbors; +// if (!map.containsKey(_val)){ +// map.put(_val, new HashSet<>()); +// } +// for (Node y : _neighbors){ +// map.get(_val).add(y); +// } +// } +// +// List 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 visited, Map> 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 visited = new HashMap <> (); + public Node cloneGraph_1(Node node) { + if (node == null) { return node; } - // build map : {node_val : node_neighbors} - Map> map = new HashMap<>(); - for (Node x : node.neighbors){ - int _val = x.val; - List _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 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 visited, Map> 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 visited = new HashMap(); + + // Put the first node in the queue + LinkedList queue = new LinkedList (); + 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); } }