Skip to content

Commit 0014ddb

Browse files
author
Botao Xiao
committed
[Function add]: 1. Add leetcode solutions with tag graph.
1 parent 8390810 commit 0014ddb

5 files changed

+377
-2
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -619,12 +619,29 @@
619619
* [935. Knight Dialer](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/935.%20Knight%20Dialer.md)
620620

621621
### Graph
622+
#### Clone graph: hashtable + dfs
622623
* [133. Clone Graph](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/133.%20Clone%20Graph.md)
623624
* [138. Copy List with Random Pointer](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/138.%20Copy%20List%20with%20Random%20Pointer.md)
625+
#### Grid + Connected Component
624626
* [200. Number of Islands](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/200.%20Number%20of%20Islands.md)
625627
* [547. Friend Circles](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/547.%20Friend%20Circles.md)
626628
* [695. Max Area of Island](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/695.%20Max%20Area%20of%20Island.md)
627629
* [733. Flood Fill](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/733.%20Flood%20Fill.md)
630+
* [827. Making A Large Island](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/827.%20Making%20A%20Large%20Island.md)
631+
* [841. Keys and Rooms](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/841.%20Keys%20and%20Rooms.md)
632+
#### Topological Sort
633+
* [207. Course Schedule](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/207.%20Course%20Schedule.md)
634+
* [210. Course Schedule II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/210.%20Course%20Schedule%20II.md)
635+
* [802. Find Eventual Safe States](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/802.%20Find%20Eventual%20Safe%20States.md)
636+
#### Union Find Set / Disjoint Set
637+
* [399. Evaluate Division](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/399.%20Evaluate%20Division.md)
638+
* [839. Similar String Groups](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/839.%20Similar%20String%20Groups.md)
639+
* [952. Largest Component Size by Common Factor](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/952.%20Largest%20Component%20Size%20by%20Common%20Factor.md)
640+
* [990. Satisfiability of Equality Equations](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/990.%20Satisfiability%20of%20Equality%20Equations.md)
641+
* [721. Accounts Merge](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/721.%20Accounts%20Merge.md)
642+
#### Bipartite
643+
* [785. Is Graph Bipartite?](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/785.%20Is%20Graph%20Bipartite?.md)
644+
* [886. Possible Bipartition](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/886.%20Possible%20Bipartition.md)
628645

629646
## Algorithm(4th_Edition)
630647
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.

leetcode/399. Evaluate Division.md

+119-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Example:
77
Given a / b = 2.0, b / c = 3.0.
88
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
9-
return [6.0, 0.5, -1.0, 1.0, -1.0 ].
9+
return [6.0, 0.5, -1.0, 1.0, -1.0 ].
1010
```
1111

1212
### Thinking:
@@ -87,4 +87,121 @@ class Solution {
8787
}
8888
}
8989
}
90-
```
90+
```
91+
92+
### Second Time
93+
* Method 1: Union Find Set
94+
```Java
95+
class Solution {
96+
private static class Node{
97+
// node = ratio * parent or node / parent = ratio
98+
String parent;
99+
double ratio;
100+
public Node(String parent, double ratio){
101+
this.parent = parent;
102+
this.ratio = ratio;
103+
}
104+
}
105+
private static class UF{
106+
Map<String, Node> uf = new HashMap<>();
107+
108+
public Node find(String s){
109+
if(!uf.containsKey(s)) return null;
110+
Node n = uf.get(s);
111+
if(!n.parent.equals(s)){
112+
Node p = find(n.parent);
113+
n.parent = p.parent;
114+
// b = 2 * a && c = 2 * b => c = 2 * 3 * a = 6 * a
115+
n.ratio *= p.ratio;
116+
}
117+
return n;
118+
}
119+
120+
public void union(String i, String j, double ratio){
121+
boolean hasI = uf.containsKey(i);
122+
boolean hasJ = uf.containsKey(j);
123+
// i / j = ratio => i = j * ratio
124+
if(!hasI && !hasJ){
125+
uf.put(i, new Node(j, ratio));
126+
uf.put(j, new Node(j, 1D));
127+
}else if(!hasI){
128+
uf.put(i, new Node(j, ratio));
129+
}else if(!hasJ){
130+
uf.put(j, new Node(i, 1 / ratio));
131+
}else{
132+
Node p = find(i);
133+
Node q = find(j);
134+
p.parent = q.parent;
135+
p.ratio = ratio * q.ratio / p.ratio;
136+
}
137+
}
138+
}
139+
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
140+
UF uf = new UF();
141+
for(int i = 0; i < equations.length; i++){
142+
uf.union(equations[i][0], equations[i][1], values[i]);
143+
}
144+
double[] res = new double[queries.length];
145+
for(int i = 0; i < res.length; i++){
146+
String[] query = queries[i];
147+
Node first = uf.find(query[0]);
148+
Node second = uf.find(query[1]);
149+
if(first == null || second == null || !first.parent.equals(second.parent)){
150+
res[i] = -1D;
151+
continue;
152+
}else{
153+
// we want a / b = first.ratio * r / second.ratio * r
154+
res[i] = first.ratio / second.ratio;
155+
}
156+
}
157+
return res;
158+
}
159+
}
160+
```
161+
162+
* Method 2: dfs: record all pairs and use dfs to get the result.
163+
```Java
164+
class Solution {
165+
Map<String, Map<String, Double>> graph;
166+
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
167+
graph = new HashMap<>();
168+
for(int i = 0; i < equations.length; i++){
169+
// a / b = ratio => a = b * ratio
170+
String first = equations[i][0], second = equations[i][1];
171+
Map<String, Double> temp = graph.containsKey(first) ? graph.get(first): new HashMap<>();
172+
temp.put(second, values[i]);
173+
graph.put(first, temp);
174+
temp = graph.containsKey(second) ? graph.get(second): new HashMap<>();
175+
temp.put(first, 1/ values[i]);
176+
graph.put(second, temp);
177+
}
178+
double[] res = new double[queries.length];
179+
for(int i = 0; i < res.length; i++){
180+
String first = queries[i][0];
181+
String second = queries[i][1];
182+
if(!graph.containsKey(first) || !graph.containsKey(second)){
183+
res[i] = -1D;
184+
continue;
185+
}else{
186+
res[i] = divide(first, second, new HashSet<>());
187+
}
188+
}
189+
return res;
190+
}
191+
private double divide(String first, String second, Set<String> visited){
192+
if(first.equals(second)) return 1D;
193+
else{
194+
for(String next : graph.get(first).keySet()){
195+
if(visited.contains(next)) continue;
196+
visited.add(next);
197+
double d = divide(next, second, visited);
198+
if(d > 0) return d * graph.get(first).get(next);
199+
}
200+
return -1D;
201+
}
202+
}
203+
}
204+
```
205+
206+
### Reference
207+
1. [花花酱 LeetCode 399. Evaluate Division](http://zxi.mytechroad.com/blog/graph/leetcode-399-evaluate-division/)
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## 827. Making A Large Island
2+
3+
### Question:
4+
In a 2D grid of 0s and 1s, we change at most one 0 to a 1.
5+
6+
After, what is the size of the largest island? (An island is a 4-directionally connected group of 1s).
7+
8+
```
9+
Example 1:
10+
11+
Input: [[1, 0], [0, 1]]
12+
Output: 3
13+
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
14+
Example 2:
15+
16+
Input: [[1, 1], [1, 0]]
17+
Output: 4
18+
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.
19+
Example 3:
20+
21+
Input: [[1, 1], [1, 1]]
22+
Output: 4
23+
Explanation: Can't change any 0 to 1, only one island with area = 4.
24+
```
25+
26+
Notes:
27+
* 1 <= grid.length = grid[0].length <= 50.
28+
* 0 <= grid[i][j] <= 1.
29+
30+
### Solution:
31+
* Method 1: dfs AC 16.04%
32+
* We check all nodes whose value is 0, we try to change this value from 0 to 1 and record the max area could be reach.
33+
```Java
34+
class Solution {
35+
private int height, width;
36+
private int res = 0;
37+
private static int[][] dir = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
38+
public int largestIsland(int[][] grid) {
39+
if(grid == null || grid.length == 0 || grid[0].length == 0) return 0;
40+
height = grid.length;
41+
width = grid[0].length;
42+
for(int i = 0; i < height; i++){
43+
for(int j = 0; j < width; j++){
44+
if(grid[i][j] == 0){ //Assume we change the current node from 0 to 1
45+
res = Math.max(dfs(grid, i, j, new boolean[height][width]), res);
46+
}
47+
}
48+
}
49+
return res == 0 ? height * width: res;
50+
}
51+
private int dfs(int[][] grid, int r, int c, boolean[][] visited){
52+
int cur = 1;
53+
int tx = 0, ty = 0;
54+
visited[r][c] = true;
55+
for(int d = 0; d < 4; d++){
56+
tx = r + dir[d][0];
57+
ty = c + dir[d][1];
58+
if(tx >= 0 && tx < height && ty >= 0 && ty < width && grid[tx][ty] == 1 && !visited[tx][ty]){
59+
cur += dfs(grid, tx, ty, visited);
60+
}
61+
}
62+
return cur;
63+
}
64+
}
65+
```
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
## 839. Similar String Groups
2+
3+
### Question
4+
Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it equals Y.
5+
6+
For example, "tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are similar, but "star" is not similar to "tars", "rats", or "arts".
7+
8+
Together, these form two connected groups by similarity: {"tars", "rats", "arts"} and {"star"}. Notice that "tars" and "arts" are in the same group even though they are not similar. Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.
9+
10+
We are given a list A of strings. Every string in A is an anagram of every other string in A. How many groups are there?
11+
12+
```
13+
Example 1:
14+
15+
Input: ["tars","rats","arts","star"]
16+
Output: 2
17+
```
18+
19+
Note:
20+
* A.length <= 2000
21+
* A[i].length <= 1000
22+
* A.length * A[i].length <= 20000
23+
* All words in A consist of lowercase letters only.
24+
* All words in A have the same length and are anagrams of each other.
25+
* The judging time limit has been increased for this question.
26+
27+
### Solution
28+
* Method 1: Union Find Set
29+
```Java
30+
class Solution {
31+
private int[] uf;
32+
private int[] rank;
33+
public int numSimilarGroups(String[] A) {
34+
uf = new int[A.length];
35+
rank = new int[A.length];
36+
for(int i = 0; i < A.length; i++){
37+
uf[i] = i;
38+
}
39+
for(int i = 0; i < A.length; i++){
40+
for(int j = i + 1; j < A.length; j++){
41+
if(i == j) continue;
42+
else if(connected(A[i], A[j])){
43+
union(i, j);
44+
}
45+
}
46+
}
47+
int res = 0;
48+
for(int i = 0; i < A.length; i++){
49+
if(uf[i] == i) res++;
50+
}
51+
return res;
52+
}
53+
private int find(int a){
54+
if(a != uf[a]){
55+
uf[a] = find(uf[a]);
56+
}
57+
return uf[a];
58+
}
59+
private void union(int a, int b){
60+
int p = find(a);
61+
int q = find(b);
62+
if(q == p) return;
63+
if(rank[p] < rank[q]){
64+
uf[p] = q;
65+
rank[q] = Math.max(rank[q], rank[p] + 1);
66+
}else{
67+
uf[q] = p;
68+
rank[p] = Math.max(rank[p], rank[q] + 1);
69+
}
70+
}
71+
private boolean connected(String a, String b){
72+
if(a.equals(b)) return true;
73+
char[] arrA = a.toCharArray();
74+
char[] arrB = b.toCharArray();
75+
int diff = 0;
76+
for(int i = 0; i < arrA.length; i++){
77+
if(arrA[i] != arrB[i]) diff++;
78+
if(diff > 2) return false;
79+
}
80+
return diff == 0 || diff == 2;
81+
}
82+
}
83+
```

0 commit comments

Comments
 (0)