From b86cbcdbad95d4a55b40b2ac23aaecb63c637101 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Mon, 11 Mar 2024 22:06:51 +0800 Subject: [PATCH] add 269 java, update progress --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +- .../LeetCodeJava/Graph/AlienDictionary.java | 161 ++++++++++++++++++ 4 files changed, 172 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/Graph/AlienDictionary.java diff --git a/README.md b/README.md index 4cf589f0..c8b7398e 100644 --- a/README.md +++ b/README.md @@ -1126,7 +1126,7 @@ | # | Title | Solution | Time | Space | Difficulty | Status | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| -269 |[Alien Dictionary](https://leetcode.com/problems/alien-dictionary/) | [Python](./leetcode_python/Graph/alien-dictionary.py) | | | Hard| Curated Top 75, dfs, bfs, topology sort, `fb`, google, m$, airbnb, uber, amazon | AGAIN******** (3) (not start) +269 |[Alien Dictionary](https://leetcode.com/problems/alien-dictionary/) | [Python](./leetcode_python/Graph/alien-dictionary.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Graph/AlienDictionary.java) | | | Hard| Curated Top 75, dfs, bfs, topology sort, `fb`, google, m$, airbnb, uber, amazon | AGAIN******** (4) (not start) 323| [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) | [Python ](./leetcode_python/Graph/number-of-connected-components-in-an-undirected-graph.py) | _O(n)_ | _O(n)_| Medium | Curated Top 75, 🔒 , dfs, Union Find, `graph`, `linkedin`, `amazon`, fb, google, m$, linkedin| AGAIN************** (5) (MUST) 959 | [Regions Cut By Slashes](https://leetcode.com/problems/regions-cut-by-slashes/) | [Python](./leetcode_python/Graph/regions-cut-by-slashes.py) | _O(n^2)_| _O(n^2)_| Medium | Union Find | 1135 | [Connecting Cities With Minimum Cost](https://leetcode.com/problems/connecting-cities-with-minimum-cost/) | [Python](./leetcode_python/Graph/connecting-cities-with-minimum-cost.py) | _O(n^2)_| _O(n^2)_| Medium | union find, Kruskal, prime, graph, amazon | AGAIN (not start) diff --git a/data/progress.txt b/data/progress.txt index 2b26acd7..8ae97876 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240311: 261(again!!!) +20240311: 261(again!!!),268,269(again) 20240310: 213,217,226,230,235,238,242,252,253 20240309: 207(again!!!),208,211(again),212 20240308: 152,153(again),190,191,198,200,206(again) diff --git a/data/to_review.txt b/data/to_review.txt index 532da222..96cd51d6 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2024-05-05 -> ['261(again!!!)'] +2024-05-05 -> ['261(again!!!),268,269(again)'] 2024-05-04 -> ['213,217,226,230,235,238,242,252,253'] 2024-05-03 -> ['207(again!!!),208,211(again),212'] 2024-05-02 -> ['152,153(again),190,191,198,200,206(again)'] @@ -10,7 +10,7 @@ 2024-04-24 -> ['39,48(again),49,53,54'] 2024-04-23 -> ['20,21,23,33(again)'] 2024-04-22 -> ['1,3,5,4,19'] -2024-04-14 -> ['261(again!!!)'] +2024-04-14 -> ['261(again!!!),268,269(again)'] 2024-04-13 -> ['213,217,226,230,235,238,242,252,253'] 2024-04-12 -> ['207(again!!!),208,211(again),212'] 2024-04-11 -> ['152,153(again),190,191,198,200,206(again)'] @@ -21,7 +21,7 @@ 2024-04-06 -> ['55(again),56,62,70'] 2024-04-03 -> ['39,48(again),49,53,54'] 2024-04-02 -> ['20,21,23,33(again)'] -2024-04-01 -> ['261(again!!!)', '1,3,5,4,19'] +2024-04-01 -> ['261(again!!!),268,269(again)', '1,3,5,4,19'] 2024-03-31 -> ['213,217,226,230,235,238,242,252,253'] 2024-03-30 -> ['207(again!!!),208,211(again),212'] 2024-03-29 -> ['152,153(again),190,191,198,200,206(again)'] @@ -29,19 +29,19 @@ 2024-03-27 -> ['121,124(again),125,128,133(again)'] 2024-03-26 -> ['100,102,104,105(again)'] 2024-03-25 -> ['73,76,79(again),91,25'] -2024-03-24 -> ['261(again!!!)', '55(again),56,62,70'] +2024-03-24 -> ['261(again!!!),268,269(again)', '55(again),56,62,70'] 2024-03-23 -> ['213,217,226,230,235,238,242,252,253'] 2024-03-22 -> ['207(again!!!),208,211(again),212'] 2024-03-21 -> ['152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54'] 2024-03-20 -> ['139(again),141,143(again)', '20,21,23,33(again)'] -2024-03-19 -> ['261(again!!!)', '121,124(again),125,128,133(again)', '1,3,5,4,19'] +2024-03-19 -> ['261(again!!!),268,269(again)', '121,124(again),125,128,133(again)', '1,3,5,4,19'] 2024-03-18 -> ['213,217,226,230,235,238,242,252,253', '100,102,104,105(again)'] 2024-03-17 -> ['207(again!!!),208,211(again),212', '73,76,79(again),91,25'] -2024-03-16 -> ['261(again!!!)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70'] +2024-03-16 -> ['261(again!!!),268,269(again)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70'] 2024-03-15 -> ['213,217,226,230,235,238,242,252,253', '139(again),141,143(again)'] -2024-03-14 -> ['261(again!!!)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)'] -2024-03-13 -> ['261(again!!!)', '213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54'] -2024-03-12 -> ['261(again!!!)', '213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] +2024-03-14 -> ['261(again!!!),268,269(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)'] +2024-03-13 -> ['261(again!!!),268,269(again)', '213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54'] +2024-03-12 -> ['261(again!!!),268,269(again)', '213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] 2024-03-11 -> ['213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19'] 2024-03-10 -> ['207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '100,102,104,105(again)'] 2024-03-09 -> ['152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '121,124(again),125,128,133(again)', '73,76,79(again),91,25'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Graph/AlienDictionary.java b/leetcode_java/src/main/java/LeetCodeJava/Graph/AlienDictionary.java new file mode 100644 index 00000000..5d1c2e5c --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/Graph/AlienDictionary.java @@ -0,0 +1,161 @@ +package LeetCodeJava.Graph; + +// https://leetcode.com/problems/alien-dictionary/description/ +// https://leetcode.ca/all/269.html + +import java.util.*; + +public class AlienDictionary { + + // V0 + // TODO : implement it +// public String alienOrder(String[] words){ +// return null; +// } + + // V1 + // IDEA : topological sorting + // https://leetcode.ca/all/269.html + // dfs + public String alienOrder_1(String[] words) { + + // Step 1: build the graph + Map> graph = new HashMap<>(); + for (int i = 0; i < words.length; i++) { + String currentWord = words[i]; + for (int j = 0; j < currentWord.length(); j++) { + if (!graph.containsKey(currentWord.charAt(j))) { + graph.put(currentWord.charAt(j), new HashSet<>()); + } + } + + if (i > 0) { + connectGraph(graph, words[i - 1], currentWord); + } + } + + // Step 2: topological sorting + StringBuffer sb = new StringBuffer(); + Map visited = new HashMap(); // mark as visited: visited.put(vertexId, -1); + + for (Map.Entry> entry: graph.entrySet()) { + char vertexId = entry.getKey(); + if (!topologicalSort(vertexId, graph, sb, visited)) { + return ""; + } + } + + return sb.toString(); + } + + private void connectGraph(Map> graph, String prev, String curr) { + if (prev == null || curr == null) { + return; + } + + int len = Math.min(prev.length(), curr.length()); + + for (int i = 0; i < len; i++) { + char p = prev.charAt(i); + char q = curr.charAt(i); + if (p != q) { // so if same duplicated work, will not reach here and not update graph + if (!graph.get(p).contains(q)) { + graph.get(p).add(q); + } + break; + } + } + } + + private boolean topologicalSort( + char vertexId, + Map> graph, + StringBuffer sb, + Map visited + ) { + + if (visited.containsKey(vertexId)) { + // visited + if (visited.get(vertexId) == -1) { // -1 meaning visited, cycle found + return false; + } + + // already in the list + if (visited.get(vertexId) == 1) { + return true; + } + } + + visited.put(vertexId, -1); // mark as visited + + + Set neighbors = graph.get(vertexId); + for (char neighbor : neighbors) { + if (!topologicalSort(neighbor, graph, sb, visited)) { + return false; + } + } + + sb.insert(0, vertexId); + visited.put(vertexId, 1); // restore visited + + return true; + } + + // V2 + // https://github.com/Cee/Leetcode/blob/master/269%20-%20Alien%20Dictionary.java + public String alienOrder_2(String[] words) { + Map> map = new HashMap<>(); + Map degree = new HashMap<>(); + String result = ""; + if (words == null || words.length == 0) { return result; } + // Degree char = 0 + for (String s: words) { + for (char c: s.toCharArray()) { + degree.put(c, 0); + } + } + + for (int i = 0; i < words.length - 1; i++) { + String curr = words[i]; + String next = words[i + 1]; + int min = Math.min(curr.length(), next.length()); + for (int j = 0; j < min; j++) { + char c1 = curr.charAt(j); + char c2 = next.charAt(j); + if (c1 != c2) { + Set set = map.getOrDefault(c1, new HashSet<>()); + if (!set.contains(c2)) { + set.add(c2); + map.put(c1, set); + degree.put(c2, degree.get(c2) + 1); // update c2, c1 < c2 + } + break; + } + } + } + + LinkedList q = new LinkedList<>(); + for (char c: degree.keySet()) { + if (degree.get(c) == 0) { + q.add(c); + } + } + + while (!q.isEmpty()) { + char c = q.poll(); + result += c; + if (map.containsKey(c)) { + for (char next: map.get(c)) { + degree.put(next, degree.get(next) - 1); + if (degree.get(next) == 0) { + q.offer(next); + } + } + } + } + + return result.length() == degree.size() ? result : ""; + } + +}