LeetCode 547 Number of Provinces - Problem Statement

The problem

Given an n × n adjacency matrix where isConnected[i][j] = 1 means cities i and j are directly connected, find the number of provinces.

A province is a group of cities connected directly or indirectly.

With isConnected = [[1,1,0],[1,1,0],[0,0,1]]:

  • Cities 0 and 1 are connected (isConnected[0][1] = 1).
  • City 2 is isolated.
  • 2 provinces: {0,1} and {2}.

With isConnected = [[1,0,0],[0,1,0],[0,0,1]]:

  • No connections between cities.
  • 3 provinces: each city is its own province.

Constraints: 1n2001 \le n \le 200.