algorithm - 如何找到 KthLargest 算法的平均情况?
问题描述
我知道最好的情况是 B(1)。最坏的情况是 W(n+1),因为在搜索列表时,因为您必须比较最后一个元素的两侧。我最困惑的是这个算法的平均情况。
findKthLargest (list, N, K)
for i = 1 to K do
largest = list[1]
largestLocation = 1
for j = 2 to N-(i-1) do
if list[j] > largest then
largest = list[j]
largestLocation = j
endIf
endFor
swap(list[N - (i-1)], list[largestLocation] )
endFor
return largest
解决方案
推荐阅读
- python - 如何聚焦光线或如何仅在 pygame 中绘制窗口的某些圆形部分?
- spring - 500内部服务器错误; 在springBoot rest api中使用POST方法时
- mysql - 万一主数据库崩溃,如何切换到从数据库?
- android - 仅对于某些图像,无法从 android 10 中的 URI 获取文件路径
- flutter - 文字溢出,无法控制
- google-cloud-platform - 命令从谷歌云存储桶中的文件中的“字符串”的grep中查找文件名?
- reactjs - 如何为函数组件创建 ref
- angular - 使用 History API 存储状态
- python-3.x - VS代码,输出未显示
- python - Kneighbors 时间序列分类