547. Number of Provinces
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to count the number of "provinces" among a group of cities. A province is defined as a group of cities that are connected either directly or indirectly. This is a classic graph problem. We can model the cities as vertices and the direct connections as edges in an undirected graph. The isConnected matrix serves as the adjacency matrix for this graph. A province is, therefore, a connected component of the graph. The problem is equivalent to finding the number of connected components in the graph.
Solution
Number of Provinces in Python
class Solution:
def findCircleNum(self, isConnected: List[List[int]]) -> int:
n = len(isConnected)
# Using a boolean array to keep track of visited cities.
visited = [False] * n
provinces = 0
# DFS function to visit all cities in a province.
def dfs(u: int):
# Mark the current city as visited.
visited[u] = True
# Visit all directly connected cities.
for v in range(n):
# If there's a connection and the city hasn't been visited.
if isConnected[u][v] == 1 and not visited[v]:
dfs(v)
# Iterate through all cities to find provinces.
for i in range(n):
# If a city hasn't been visited, it's the start of a new province.
if not visited[i]:
provinces += 1
dfs(i)
return provinces
Number of Provinces in Java
class Solution {
public int findCircleNum(int[][] isConnected) {
int n = isConnected.length;
if (n == 0) {
return 0;
}
boolean[] visited = new boolean[n];
int provinces = 0;
// Iterate through all cities.
for (int i = 0; i < n; i++) {
// If a city has not been visited, it's the start of a new province.
if (!visited[i]) {
provinces++;
// Use DFS to find all cities in this province.
dfs(i, n, isConnected, visited);
}
}
return provinces;
}
private void dfs(int u, int n, int[][] isConnected, boolean[] visited) {
// Mark the current city as visited.
visited[u] = true;
// Visit all directly connected cities.
for (int v = 0; v < n; v++) {
// If there's a connection and the city hasn't been visited yet.
if (isConnected[u][v] == 1 && !visited[v]) {
dfs(v, n, isConnected, visited);
}
}
}
}
Number of Provinces in C++
class Solution {
public:
void dfs(int u, int n, const vector<vector<int>>& isConnected, vector<bool>& visited) {
// Mark the current city as visited.
visited[u] = true;
// Visit all directly connected cities.
for (int v = 0; v < n; ++v) {
// If there is a connection and the city hasn't been visited yet.
if (isConnected[u][v] == 1 && !visited[v]) {
dfs(v, n, isConnected, visited);
}
}
}
int findCircleNum(vector<vector<int>>& isConnected) {
int n = isConnected.size();
if (n == 0) {
return 0;
}
vector<bool> visited(n, false);
int provinces = 0;
// Iterate through all cities.
for (int i = 0; i < n; ++i) {
// If a city has not been visited, it's a new province.
if (!visited[i]) {
provinces++;
// Start DFS to find all cities in this province.
dfs(i, n, isConnected, visited);
}
}
return provinces;
}
};
Number of Provinces in TypeScript
function findCircleNum(isConnected: number[][]): number {
const n = isConnected.length;
if (n === 0) {
return 0;
}
const visited: boolean[] = new Array(n).fill(false);
let provinces = 0;
const dfs = (u: number): void => {
// Mark the current city as visited.
visited[u] = true;
// Visit all directly connected cities.
for (let v = 0; v < n; v++) {
// If there is a connection and the city hasn't been visited yet.
if (isConnected[u][v] === 1 && !visited[v]) {
dfs(v);
}
}
};
// Iterate through all cities.
for (let i = 0; i < n; i++) {
// If a city has not been visited, it's a new province.
if (!visited[i]) {
provinces++;
// Start DFS to find all cities in this province.
dfs(i);
}
}
return provinces;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.