Medium
Number of Provinces
Medium
97 submissions
25 coins
+100 XP
Graph
Union Find
Problem Description
`isConnected` is an `n × n` symmetric matrix where `isConnected[i][j] = 1` means cities `i` and `j` are directly linked. A province is a group of cities connected directly or indirectly. Return the 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
```
**Example 3:**
```
Input: isConnected = [[1]]
Output: 1
```
Constraints
- `1 <= n <= 200`
- `isConnected[i][i] = 1`
Need help?
Connect with expert programmers for real-time collaborative coding, video meetings, and whiteboard sessions via CodeConnect.
Video Call
Whiteboard
Live Coding
Screen Share