547. Number of Provinces LeetCode Solution
In this guide, you will get 547. Number of Provinces LeetCode Solution with the best time and space complexity. The solution to Number of Provinces problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Number of Provinces solution in C++
- Number of Provinces solution in Java
- Number of Provinces solution in Python
- Additional Resources
Problem Statement of Number of Provinces
There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.
A province is a group of directly or indirectly connected cities and no other cities outside of the group.
You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise.
Return the total number of provinces.
Example 1:
Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2
Example 2:
Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3
Constraints:
1 <= n <= 200
n == isConnected.length
n == isConnected[i].length
isConnected[i][j] is 1 or 0.
isConnected[i][i] == 1
isConnected[i][j] == isConnected[j][i]
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n)
547. Number of Provinces LeetCode Solution in C++
class UnionFind {
public:
UnionFind(int n) : count(n), id(n), rank(n) {
iota(id.begin(), id.end(), 0);
}
void unionByRank(int u, int v) {
const int i = find(u);
const int j = find(v);
if (i == j)
return;
if (rank[i] < rank[j]) {
id[i] = j;
} else if (rank[i] > rank[j]) {
id[j] = i;
} else {
id[i] = j;
++rank[j];
}
--count;
}
int getCount() const {
return count;
}
private:
int count;
vector<int> id;
vector<int> rank;
int find(int u) {
return id[u] == u ? u : id[u] = find(id[u]);
}
};
class Solution {
public:
int findCircleNum(vector<vector<int>>& isConnected) {
const int n = isConnected.size();
UnionFind uf(n);
for (int i = 0; i < n; ++i)
for (int j = i; j < n; ++j)
if (isConnected[i][j] == 1)
uf.unionByRank(i, j);
return uf.getCount();
}
};
/* code provided by PROGIEZ */
547. Number of Provinces LeetCode Solution in Java
class UnionFind {
public UnionFind(int n) {
count = n;
id = new int[n];
rank = new int[n];
for (int i = 0; i < n; ++i)
id[i] = i;
}
public void unionByRank(int u, int v) {
final int i = find(u);
final int j = find(v);
if (i == j)
return;
if (rank[i] < rank[j]) {
id[i] = j;
} else if (rank[i] > rank[j]) {
id[j] = i;
} else {
id[i] = j;
++rank[j];
}
--count;
}
public int getCount() {
return count;
}
private int count;
private int[] id;
private int[] rank;
private int find(int u) {
return id[u] == u ? u : (id[u] = find(id[u]));
}
}
class Solution {
public int findCircleNum(int[][] isConnected) {
final int n = isConnected.length;
UnionFind uf = new UnionFind(n);
for (int i = 0; i < n; ++i)
for (int j = i; j < n; ++j)
if (isConnected[i][j] == 1)
uf.unionByRank(i, j);
return uf.getCount();
}
}
// code provided by PROGIEZ
547. Number of Provinces LeetCode Solution in Python
class UnionFind:
def __init__(self, n: int):
self.count = n
self.id = list(range(n))
self.rank = [0] * n
def unionByRank(self, u: int, v: int) -> None:
i = self._find(u)
j = self._find(v)
if i == j:
return
if self.rank[i] < self.rank[j]:
self.id[i] = j
elif self.rank[i] > self.rank[j]:
self.id[j] = i
else:
self.id[i] = j
self.rank[j] += 1
self.count -= 1
def _find(self, u: int) -> int:
if self.id[u] != u:
self.id[u] = self._find(self.id[u])
return self.id[u]
class Solution:
def findCircleNum(self, isConnected: list[list[int]]) -> int:
n = len(isConnected)
uf = UnionFind(n)
for i in range(n):
for j in range(i, n):
if isConnected[i][j] == 1:
uf.unionByRank(i, j)
return uf.count
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.