LeetCode 547. Friend Circles

原题链接在这里:https://leetcode.com/problems/friend-circles/description/

题目:

There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.

Given a N*N matrix M representing the friend relationship between students in the class. If M[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.

Example 1:

Input: 
[[1,1,0],
 [1,1,0],
 [0,0,1]]
Output: 2
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle. 
The 2nd student himself is in a friend circle. So return 2.

Example 2:

Input: 
[[1,1,0],
 [1,1,1],
 [0,1,1]]
Output: 1
Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends, 
so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.

Note:

  1. N is in range [1,200].
  2. M[i][i] = 1 for all students.
  3. If M[i][j] = 1, then M[j][i] = 1.

题解:

Number of Connected Components in an Undirected Graph类似. 可以采用DFS, BFS, Union Find三种方法.

当M[i][j] == 1. 就说明i 和 j是好友. DFS, BFS时标记走过的点即可.

Time Complexity: O(n^2). n = M.length. M上的点最多走两遍.

Space: O(n). 开了visited array标记. 最多用了n层stack.

AC Java:

 1 class Solution {
 2     public int findCircleNum(int[][] M) {
 3         if(M == null || M.length == 0 || M[0].length == 0){
 4             return 0;
 5         }
 6         
 7         int len = M.length;
 8         boolean [] visited = new boolean[len];
 9         int res = 0;
10         for(int i = 0; i<len; i++){
11             if(!visited[i]){
12                 dfs(M, visited, i);
13                 res++;
14             }
15         }
16         
17         return res;
18     }
19     
20     private void dfs(int [][] M, boolean [] visited, int i){
21         for(int j = 0; j<M[0].length; j++){
22             if(!visited[j] && M[i][j]==1){
23                 visited[j] = true;
24                 dfs(M, visited, j);
25             }
26         }
27     }
28 }

BFS可以选择在出queue的时候更改标记.

Time Complexity: O(n^2). n = M.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int findCircleNum(int[][] M) {
 3         if(M == null || M.length == 0 || M[0].length == 0){
 4             return 0;
 5         }
 6         
 7         int res = 0;
 8         int len = M.length;
 9         boolean [] visited = new boolean[len];
10         LinkedList<Integer> que = new LinkedList<Integer>();
11         for(int i = 0; i<len; i++){
12             if(!visited[i]){
13                 res++;
14                 que.add(i);
15                 while(!que.isEmpty()){
16                     int cur = que.poll();
17                     visited[cur] = true;
18                     for(int j = 0; j<len; j++){
19                         if(!visited[j] && M[cur][j]==1){
20                             que.add(j);
21                         }
22                     }
23                 }
24             }
25         }
26         
27         return res;
28     }
29 }

Union Find Methos is to return the count of unions.

Time Complexity: O(n^2logn). find takes O(logn). With path compression and union by weight, amatorize O(1).

Space: O(n).

AC Java:

 1 class Solution {
 2     public int findCircleNum(int[][] M) {
 3         if(M == null || M.length == 0 || M[0].length == 0){
 4             return 0;
 5         }
 6         
 7         int n = M.length;
 8         UnionFind uf = new UnionFind(n);
 9         for(int i = 0; i<n; i++){
10             for(int j = 0; j<i; j++){
11                 if(i!=j && M[i][j] == 1){
12                     if(uf.find(i) != uf.find(j)){
13                         uf.union(i, j);
14                     }
15                 }
16             }
17         }
18         
19         return uf.count;
20     }
21 }
22 
23 class UnionFind{
24     int [] parent;
25     int [] size;
26     int count;
27     
28     public UnionFind(int n){
29         this.parent = new int[n];
30         this.size = new int[n];
31         this.count = n;
32         
33         for(int i = 0; i<n; i++){
34             parent[i] = i;
35             size[i] = 1;
36         }
37     }
38     
39     public int find(int i){
40         while(i != parent[i]){
41             parent[i] = parent[parent[i]];
42             i = parent[i];
43         }
44         
45         return parent[i];
46     }
47     
48     public void union(int i, int j){
49         int x = find(i);
50         int y = find(j);
51         
52         if(size[x] > size[y]){
53             parent[y] = x;
54             size[x] += size[y];
55         }else{
56             parent[x] = y;
57             size[y] += size[x];
58         }
59         
60         this.count--;
61     }
62 }

类似The Earliest Moment When Everyone Become Friends.

posted @ 2018-01-01 07:15  Dylan_Java_NYC  阅读(567)  评论(0编辑  收藏  举报