1988coder

547. Friend Circles

Feb 25th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. // LeetCode URL: https://leetcode.com/problems/friend-circles/
  2.  
  3. /**
  4.  * Using Union Find
  5.  *
  6.  * Time Complexity: O(N^2 * ⍺). Where ⍺ is a very slow growing function, which
  7.  * is always ≤ 4. Thus the time complexity becomes O(N^2).
  8.  *
  9.  * Space Complexity: O(N)
  10.  *
  11.  * N = Number of students in the class.
  12.  */
  13. class Solution1 {
  14.  
  15.     class UnionFind {
  16.         int[] parent;
  17.         int[] rank;
  18.  
  19.         public UnionFind(int n) {
  20.             parent = new int[n];
  21.             rank = new int[n];
  22.             for (int i = 0; i < n; i++) {
  23.                 parent[i] = i;
  24.                 rank[i] = 1;
  25.             }
  26.         }
  27.  
  28.         public int findParent(int n) {
  29.             if (parent[n] != n) {
  30.                 parent[n] = findParent(parent[n]);
  31.             }
  32.             return parent[n];
  33.         }
  34.  
  35.         public boolean union(int a, int b) {
  36.             int pA = findParent(a);
  37.             int pB = findParent(b);
  38.  
  39.             if (pA == pB) {
  40.                 return false;
  41.             }
  42.  
  43.             if (rank[pA] >= rank[pB]) {
  44.                 parent[pB] = pA;
  45.                 rank[pA] = rank[pA] == rank[pB] ? rank[pA] + 1 : rank[pA];
  46.             } else {
  47.                 parent[pA] = pB;
  48.             }
  49.  
  50.             return true;
  51.         }
  52.     }
  53.  
  54.     public int findCircleNum(int[][] M) {
  55.         if (M == null || M.length == 0 || M[0].length == 0) {
  56.             return 0;
  57.         }
  58.  
  59.         int N = M.length;
  60.         UnionFind uf = new UnionFind(N);
  61.         int count = N;
  62.  
  63.         for (int i = 0; i < N; i++) {
  64.             for (int j = 0; j < N; j++) {
  65.                 if (i == j || M[i][j] == 0) {
  66.                     continue;
  67.                 }
  68.                 if (uf.union(i, j)) {
  69.                     count--;
  70.                 }
  71.             }
  72.         }
  73.  
  74.         return count;
  75.     }
  76. }
  77.  
  78. /**
  79.  * Using DFS
  80.  *
  81.  * Time Complexity: O(N^2)
  82.  *
  83.  * Space Complexity: O(N). Visted array Size. Also recursion stack cannot grow
  84.  * more than size N
  85.  *
  86.  * N = Number of strudents in the class.
  87.  */
  88. class Solution2 {
  89.     public int findCircleNum(int[][] M) {
  90.         if (M == null || M.length == 0 || M[0].length == 0) {
  91.             return 0;
  92.         }
  93.  
  94.         boolean[] visited = new boolean[M.length];
  95.         int count = 0;
  96.  
  97.         for (int i = 0; i < M.length; i++) {
  98.             if (!visited[i]) {
  99.                 dfsHelper(M, visited, i);
  100.                 count++;
  101.             }
  102.         }
  103.  
  104.         return count;
  105.     }
  106.  
  107.     public void dfsHelper(int[][] M, boolean[] visited, int i) {
  108.         visited[i] = true;
  109.         for (int j = 0; j < M.length; j++) {
  110.             if (visited[j] || M[i][j] == 0) {
  111.                 continue;
  112.             }
  113.             dfsHelper(M, visited, j);
  114.         }
  115.     }
  116. }
Add Comment
Please, Sign In to add comment