Leetcode 2421. Number of Good Paths - Union-Find; Google Interview Question?

preview_player
Показать описание
See other videos @codeyao9796
Python "union-find" solution to Leetcode 2421 - Number of Good Paths. The idea is simple: besides counting the single-node good paths whose total number equals the number of nodes, We compute for each unique node val v, the number of good paths of length greater than 1 and with two-ends being nodes of value v. Because in the process, we need to track subtrees (or connected components in graph language, or clusters in union-find terminology), we use union-find.
Рекомендации по теме