Number of Provinces

Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2
Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3
def findCircleNum(self, isConnected):
:type isConnected: List[List[int]]
:rtype: int
DFS the matrix, whenever we run into to a 1, run DFS and clear that island with ans + 1
ans = 0
rowLen = len(isConnected)

visited = [False]*rowLen
def dfs(i):
visited[i] = True
for j in range(rowLen):
if isConnected[i][j] == 1 and not visited[j]:

for i in range(rowLen):
if not visited[i]:
ans += 1

return ans




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Angularjs — Front End Developer FeeUseCourses

Making a basic map based application using React Native (and some Expo)

Colorful Console Message

Tackling UI complexity with State Machines

Web Dev Streaks Day- 32 (Milestone 5: Integrate Javascript)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Newbie Developer

Newbie Developer

More from Medium

Vincentian Change: Personal and Systemic Reinvention

5 Vines About Passover Hotel That You Need to See

These effective and useful tools are indispensable for capitalizing on opportunities in the NFT…