cp-wiki icon indicating copy to clipboard operation
cp-wiki copied to clipboard

Leetcode 第231场周赛题解

Open utterances-bot opened this issue 3 years ago • 2 comments

Leetcode 第231场周赛题解 | CP Wiki

lucifer1004的CP笔记

https://cp-wiki.vercel.app/tutorial/leetcode/WC231/

utterances-bot avatar Mar 07 '21 06:03 utterances-bot

想请教下大佬,21-28行的时间复杂度怎么计算的呀?看上去套了三重循环,不太会分析呀

codelh7 avatar Mar 07 '21 06:03 codelh7

@codelh7 注意所有的groups[i]最多有N个元素,所以最内层循环的执行次数最多是N次。

这种分析就类似于图的遍历:

for (int u = 0; u < n; ++u) {
  for (int v : adj[u]) {
    // do something...
  }
}

看上去是两层循环,实际上内层执行的总次数为O(E)次。

lucifer1004 avatar Mar 07 '21 06:03 lucifer1004