DIY: Number of Provinces
Discover how to determine the total number of provinces from a matrix representing direct city connections. This lesson guides you to implement efficient solutions to map connected cities and count distinct provinces using Python.
We'll cover the following...
We'll cover the following...
Problem statement
Suppose there are n cities, where some are connected, while others are not.
If a city c1 is connected to city c2, and city c2 is connected to city c3, then c1 is indirectly connected to c3.
A group of directly or indirectly connected cities, where no other city is part of this group, is called a province.
You are given a matrix, is_connected with a size of n x n. An entry is_connected[i][j] = 1 if the ...