Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
并查集的按秩合并:在按秩合并的实现中,当两个树高度相等时,将一棵树合并到另一棵树,并将高度加1。这个实现是正确的,但注意在路径压缩后,树的高度并不是准确的,不过按秩合并仍然可以作为一个优化,因为路径压缩不会使树的高度增加,所以按秩合并仍然有效。
性能考虑:使用优先队列来排序每个连通分量中的字符,最坏情况下,如果整个字符串都在一个连通分量中,那么优先队列的插入和删除操作的时间复杂度为O(n log n)。整体算法的时间复杂度主要受并查集操作和优先队列操作影响,并查集操作接近O(n),而优先队列操作最坏为O(n log n)。所以整体是O(n log n)级别,可以接受。