Skip to content

Commit 02b81c3

Browse files
committed
547_Friend_Circles
1 parent d58b053 commit 02b81c3

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Also, there are open source implementations for basic data structs and algorithm
153153
| 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/482_License_Key_Formatting.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/482_License_Key_Formatting.java) | String processing, lower and len % K, O(n) and O(n) |
154154
| 538 | [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/538_Convert_BST_to_Greater_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/538_Convert_BST_to_Greater_Tree.java) | Right first DFS with a variable recording sum of node.val and right.val. 1. Recursive.<br>2. Stack 3. Reverse Morris In-order Traversal |
155155
| 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/543_Diameter_of_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/543_Diameter_of_Binary_Tree.java) | DFS with O(1) for max answer |
156-
| 547 | [Friend Circles](https://leetcode.com/problems/friend-circles/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/547_Friend_Circles.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/547_Friend_Circles.java) | 1. DFS, O(n^2) and O(1)<br>2. BFS, O(n^2) and O(1)<br>3. Union-find, |
156+
| 547 | [Friend Circles](https://leetcode.com/problems/friend-circles/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/547_Friend_Circles.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/547_Friend_Circles.java) | 1. DFS, O(n^2) and O(1)<br>2. BFS, O(n^2) and O(1)<br>3. Union-find, O(n^2) and O(n)|
157157
| 557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/557_Reverse_Words_in_a_String_III.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/557_Reverse_Words_in_a_String_III.java) | String handle: Split with space than reverse word, O(n) and O(n). Better solution is that reverse can be O(1) space in array. |
158158
| 560 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/560_Subarray_Sum_Equals_K.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/560_Subarray_Sum_Equals_K.java) | Note that there are n^2 possible pairs, so the key point is accelerate computation for sum and reduce unnecessary pair. 1. Cummulative sum, O(n^2) and O(1)/O(n)<br>2. Add sum into hash, check if sum - k is in hash, O(n) and O(n) |
159159
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/572_Subtree_of_Another_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/572_Subtree_of_Another_Tree.java) | 1. Tree traverse and compare<br>2. Tree to string and compare |

java/547_Friend_Circles.java

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
public class Solution {
2+
public void dfs(int[][] M, int[] visited, int i) {
3+
for (int j = 0; j < M.length; j++) {
4+
if (M[i][j] == 1 && visited[j] == 0) {
5+
visited[j] = 1;
6+
dfs(M, visited, j);
7+
}
8+
}
9+
}
10+
public int findCircleNum(int[][] M) {
11+
// DFS
12+
int[] visited = new int[M.length];
13+
int count = 0;
14+
for (int i = 0; i < M.length; i++) {
15+
if (visited[i] == 0) {
16+
dfs(M, visited, i);
17+
count++;
18+
}
19+
}
20+
return count;
21+
}
22+
23+
/*public int findCircleNum(int[][] M) {
24+
// BFS
25+
int[] visited = new int[M.length];
26+
int count = 0;
27+
Queue < Integer > queue = new LinkedList < > ();
28+
for (int i = 0; i < M.length; i++) {
29+
if (visited[i] == 0) {
30+
queue.add(i);
31+
while (!queue.isEmpty()) {
32+
int s = queue.remove();
33+
visited[s] = 1;
34+
for (int j = 0; j < M.length; j++) {
35+
if (M[s][j] == 1 && visited[j] == 0)
36+
queue.add(j);
37+
}
38+
}
39+
count++;
40+
}
41+
}
42+
return count;
43+
}*/
44+
45+
/*
46+
// Union find
47+
int find(int parent[], int i) {
48+
if (parent[i] == -1)
49+
return i;
50+
return find(parent, parent[i]);
51+
}
52+
53+
void union(int parent[], int x, int y) {
54+
int xset = find(parent, x);
55+
int yset = find(parent, y);
56+
if (xset != yset)
57+
parent[xset] = yset;
58+
}
59+
public int findCircleNum(int[][] M) {
60+
int[] parent = new int[M.length];
61+
Arrays.fill(parent, -1);
62+
for (int i = 0; i < M.length; i++) {
63+
for (int j = 0; j < M.length; j++) {
64+
if (M[i][j] == 1 && i != j) {
65+
union(parent, i, j);
66+
}
67+
}
68+
}
69+
int count = 0;
70+
for (int i = 0; i < parent.length; i++) {
71+
if (parent[i] == -1)
72+
count++;
73+
}
74+
return count;
75+
}*/
76+
}

python/547_Friend_Circles.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
class Solution(object):
2+
def findCircleNum(self, M):
3+
"""
4+
:type M: List[List[int]]
5+
:rtype: int
6+
"""
7+
# because
8+
visited = [0] * len(M)
9+
count = 0
10+
for i in range(len(M)):
11+
if visited[i] == 0:
12+
self.dfs(M, visited, i)
13+
count += 1
14+
return count
15+
16+
def dfs(self, M, visited, i):
17+
for j in range(len(M)):
18+
if M[i][j] == 1 and visited[j] == 0:
19+
visited[j] = 1
20+
self.dfs(M, visited, j)
21+
22+
# def findCircleNum(self, M):
23+
# # BFS
24+
# visited = [0] * len(M)
25+
# count = 0
26+
# queue = []
27+
# for i in range(len(M)):
28+
# if visited[i] == 0:
29+
# queue.append(i)
30+
# while queue:
31+
# curr = queue.pop(0)
32+
# visited[curr] = 1
33+
# for j in range(len(M)):
34+
# if M[curr][j] == 1 and visited[j] == 0:
35+
# queue.append(j)
36+
# count += 1
37+
# return count
38+
39+
# def findCircleNum(self, M):
40+
# # Union Find
41+
# union = Union()
42+
# for i in range(len(M)):
43+
# union.add(i)
44+
# for i in range(len(M)):
45+
# for j in range(i + 1, len(M)):
46+
# if M[i][j] == 1:
47+
# union.union(i, j)
48+
# return union.count
49+
50+
# class Union(object):
51+
# """
52+
# weighted quick union find
53+
# """
54+
# def __init__(self):
55+
# # both dic and list is fine
56+
# self.id = {}
57+
# self.sz = {}
58+
# self.count = 0
59+
60+
# def count(self):
61+
# return self.count
62+
63+
# def connected(self, p, q):
64+
# return self.find(p) == self.find(q)
65+
66+
# def add(self, p):
67+
# # init
68+
# self.id[p] = p
69+
# self.sz[p] = 1
70+
# self.count += 1
71+
72+
# def find(self, p):
73+
# """
74+
# find root of p, and compress path
75+
# """
76+
# while p != self.id[p]:
77+
# self.id[p] = self.id[self.id[p]]
78+
# p = self.id[p]
79+
# return p
80+
81+
# def union(self, p, q):
82+
# """
83+
# connect p and q
84+
# """
85+
# i, j = self.find(p), self.find(q)
86+
# if i == j:
87+
# return
88+
# if self.sz[i] > self.sz[j]:
89+
# i, j = j, i
90+
# self.id[i] = j
91+
# self.sz[j] += self.sz[i]
92+
# self.count -= 1

0 commit comments

Comments
 (0)