Skip to content

Commit

Permalink
add 269 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 11, 2024
1 parent 6eaf29f commit b86cbcd
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -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)']
Expand All @@ -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)']
Expand All @@ -21,27 +21,27 @@
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)']
2024-03-28 -> ['139(again),141,143(again)']
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']
Expand Down
161 changes: 161 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Graph/AlienDictionary.java
Original file line number Diff line number Diff line change
@@ -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<Character, Set<Character>> 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<Character, Integer> visited = new HashMap<Character, Integer>(); // mark as visited: visited.put(vertexId, -1);

for (Map.Entry<Character, Set<Character>> entry: graph.entrySet()) {
char vertexId = entry.getKey();
if (!topologicalSort(vertexId, graph, sb, visited)) {
return "";
}
}

return sb.toString();
}

private void connectGraph(Map<Character, Set<Character>> 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<Character, Set<Character>> graph,
StringBuffer sb,
Map<Character, Integer> 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<Character> 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<Character, Set<Character>> map = new HashMap<>();
Map<Character, Integer> 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<Character> 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<Character> 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 : "";
}

}

0 comments on commit b86cbcd

Please sign in to comment.