Solution 1, using a SciPy function:
import scipy.sparse class Solution(object): def findCircleNum(self, M): return scipy.sparse.csgraph.connected_components(M)
Solution 2, compute the transitive closure of the (boolean) matrix and count the number of different rows:
import numpy as np class Solution(object): def findCircleNum(self, M): return len(set(map(tuple, (np.matrix(M, dtype='bool')**len(M)).A)))
I thought I knew something about Python, and then realized I know nothing about Python at all.
@realisking SciPy is an open source Python library used for scientific computing and technical computing. NumPy and SciPy are the most common lib Data Analysts use. And they made Python very popular.
Thanks for point that out!
@shawngao Yeah, it's not part of Python. Might've gotten installed here when LeetCode added problems involving randomization, to use it in the judge for checking the randomness of solutions. At least I think that's where I noticed it first.
@realisking Just added another one which uses more Python...
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.