Number of Provinces
You are given an n x n matrix isConnected where isConnected[i][j] == 1 means city i and city j are directly connected. A province is a group of cities that are directly or indirectly connected and not connected to any city outside the group. Return the total number of provinces.
Open official problem prompt ↗Count how many separate friend-groups (provinces) the cities fall into.
Picture people at a party. You tap the first person and everyone they know, and everyone those people know, all join one huddle. When a huddle is complete you move to the next untouched person and start a new huddle. The number of huddles is the answer.
- Input
- isConnected = [[1,1,0],[1,1,0],[0,0,1]]
- Output
- 2
- Why
- Cities 0 and 1 are directly connected forming one province; city 2 is isolated forming a second, so there are 2 provinces.
1 <= n <= 200n == isConnected.lengthn == isConnected[i].lengthisConnected[i][j] is 1 or 0isConnected[i][i] == 1isConnected[i][j] == isConnected[j][i]