gpt4 book ai didi

performance - 算法的最差和平均复杂度?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:25:36 27 4
gpt4 key购买 nike

输入是一个列表 L,其中包含多个 1(或无),后跟多个 2(或无)。下面的算法找到 1 的个数。对于一般情况,假设 L 包含 1 的机会均等。

A(L):
n=L.length
m=sqrt(n)
p=m-1
while p<n and L[p]=1
p+=m
p-=m+1
while p<n and L[p]=1
p+=1
return p

最佳答案

只需将列表平分即可解决 O(log2(n)) 问题。

至于你这里的算法,它的复杂度大约是 O(2sqrt(n))。

关于performance - 算法的最差和平均复杂度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26318860/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com