number-of-provinces.sh — zsh

Number of Provinces

medium
graphunion-find

You are given an n x n matrix isConnected where isConnected[i][j] is 1 when city i and city j are directly connected and 0 otherwise. A province is a group of cities that are connected directly or through other cities. Return how many provinces exist in total.

Examples
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
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.