547. Number of Provinces

547. Number of Provinces

There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.

A province is a group of directly or indirectly connected cities and no other cities outside of the group.

You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise.

Return the total number of provinces.

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

Constraints:

  • 1 <= n <= 200

  • n == isConnected.length

  • n == isConnected[i].length

  • isConnected[i][j] is 1 or 0.

  • isConnected[i][i] == 1

  • isConnected[i][j] == isConnected[j][i]

My Solutions:

这道题和number of islands不一样。 matrix[i][j]=1 代表i和j城市是相连的,他们在同一个省。求有多少个省。

方法1:DFS

遍历每个人,如果这个人之前没有访问过,说明有多一个新的朋友圈,答案记录加1。就从这个点作为起点做一次DFS,找出所有的直接朋友与间接朋友,并把他们标记访问。

Space: 一共最多N个人,每个人递归一次,所以递归空间是O(N), 标记数组visited也是O(N)。

Time:每个人递归一次,时间O(N);递归时候会遍历他的所有朋友,遍历朋友是O(N)。 总复杂度O(N^2)

方法2:BFS

  • 遍历每个人,如果这个人之前没有访问过,说明有多一个新的朋友圈,答案记录加1。就从这个点作为起点做一次BFS,找出所有的直接朋友与间接朋友,并把他们标记访问。

  • BFS流程

    • 将起点压入队列,标记访问

    • 取出队首,从队首向外找朋友,看都能扩展到哪些还没访问的朋友,压入队列并标记访问

    • 重复执行上一步,直到队列为空

时间和空间复杂度和方法1一样

方法3:用disjointed set/union find

以下是用union find的模版

也可以简化一下

Last updated