Open fockspaces opened 11 months ago
class Solution:
def findCircleNum(self, isConnected: List[List[int]]) -> int:
def find_all_cities(src):
candidates = isConnected[src]
for city, can_go in enumerate(candidates):
if can_go == 1 and city not in visited:
visited.add(city)
find_all_cities(city)
visited, provinces = set(), 0
for city in range(len(isConnected)):
if city not in visited:
find_all_cities(city)
provinces += 1
return provinces
for graph, It's convinient to use DFS and set to record the visted places