You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you group elements into components (in methods components and component_mapping), the time complexity of your current way of doing it is O(N x M) (where N is len(self._elts) and M is len(distinct_roots)).
O(N x M) because looping over distinct roots is O(M) and boolean indexing over elements elts[roots == root] is O(N).
The optimized code below using a defaultdict (hashmap) has time complexity O(N). The higher M is, the bigger is the speed up.