Skip to content

Latest commit

 

History

History
137 lines (117 loc) · 3.62 KB

547. Friend Circles.md

File metadata and controls

137 lines (117 loc) · 3.62 KB

leetcode-cn Daily Challenge on January 7th, 2021.


Difficulty : Medium

Related Topics : DFSUnion Find


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.

Constraints:

  • 1 <= N <= 200
  • M[i][i] == 1
  • M[i][j] == M[j][i]

Solution

  • mine
    • Java
      • DFS Runtime: 1 ms, faster than 75.88%, Memory Usage: 39.8 MB, less than 14.90% of Java online submissions

        //O(N^2)time
        //O(1)space
        public int findCircleNum(int[][] M) {
            int res = 0;
            int n = M.length;
            boolean[] record = new boolean[n];
            LinkedList<Integer> list = new LinkedList<>();
            for(int i = 0; i < n; i++){
                if(record[i]) continue;
                res++;
                record[i] = true;
                list.add(i);
                while(!list.isEmpty()){
                    int index = list.removeFirst();
                    for(int j = 0; j < n; j++){
                        if(M[index][j] != 1 || record[j]) continue;
                        record[j] = true;
                        list.add(j);
                    }
                }
            }
            return res;
        }
        
      • UnionFind Runtime: 1 ms, faster than 75.88%, Memory Usage: 40.5 MB, less than 14.90% of Java online submissions

        //O(N^2)time
        //O(1)space
        public int findCircleNum(int[][] M) {
            int n = M.length;
            UnionFind uf = new UnionFind(n);
            for(int i = 0; i < n; i++){
                for(int j = 0; j < n; j++){
                    if(M[i][j] == 1) uf.union(i, j);
                }
            }
            return uf.size();
        }
        
        class UnionFind{
            int size;
            int[] arr;
        
            public UnionFind(int n){
                size = n;
                arr = new int[n];
                for(int i = 0; i < n; i++){
                    arr[i] = i;
                }
            }
        
            int find(int a){
                if(a != arr[a]){
                    arr[a] = find(arr[a]);
                }
                return arr[a];
            }
        
            boolean union(int a, int b){
                if(find(a) != find(b)){
                    arr[find(a)] = find(b);
                    size--;
                    return true;
                }
                return false;
            }
        
            boolean united(){
                return size == 1;
            }
        
            int size(){
                return size;
            }
        }