Solution: Number of Provinces

Let’s solve the Number of Provinces problem using the Union Find pattern.

Statement

Given nn cities, some are directly connected, and others are not. You are given an n×nn \times n matrix called cities, where cities[i][j] =1= 1 means city ithi^{th} and city jthj^{th} are directly connected, and cities[i][j] =0= 0 ...