zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    2015/8/3 接着跌
    2015/7/31 由于昨天上升缺乏量的支持,今天横盘;在箱体下边缘稍微买了一点---错误!;复文《揭秘主力坐庄流程 内幕超乎想象》,
    打包jar类库与使用jar类库
    java eclipse 监视选择指定变量
    2015/7/29 (高开,V形反转,各种指标背离——可惜没买进,填补空缺图形的心理分析)
    XP、win7下Excel 2007多窗口打开Excel的解决方法
    2015/7/28(总结昨天抄底操作失败-割肉自保)
    六首失传股诗教你如何抄底和逃顶
    2015/7/27 (主力流出-1200亿,上周五回踩,今天到底是震荡下行,还是红魔呢?——在周五成功逃顶,结果今天回调的时候被套!——教训!)
    java中byte数组与int,long,short间的转换
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/8165984.html
Copyright © 2011-2022 走看看