Skip to interview prep
Apexed

96. Number of Provinces (Adjacency Matrix)

MediumGraphs

isConnected is an n × n matrix where isConnected[i][j] = 1 means cities i and j are directly linked (the matrix is symmetric and the diagonal is 1). A province is a group of cities linked directly or indirectly. Return the number of provinces.

Example 1

Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2

Constraints

  • 1 ≤ n ≤ 200

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Number of Provinces (Adjacency Matrix) — coding interview practice — Apexed