- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题 - 接近排序的数组 - 给定一个包含 n 个元素的数组,其中每个元素与它在排序数组中的实际位置最多相差 K 个位置,设计一个在 O(nLogK) 时间内排序的算法。
Approach - I divide the array in n/K elements each(n/k + 1 , if n%k!=0).
Then I run a loop n/k times ,inside which I sort eack n/k group using
MergeSort(Complexity = KLogK).So complexity for the loop is O(nLogK).
Finally I merge the n/k groups using a Merge Function(similar to Merging
K Sorted arrays, complexity = nLog(n/k)).
So overall complexity is between nLogK and nLog(n/K) but I have to
achieve complexity O(nLogK).
Comparing K and n/K depends on values of n and K.
谁能帮我完成最后的合并操作或更好的方法。
PS:当时我不知道堆或队列,所以我正在寻找不涉及这些的解决方案。
最佳答案
首先,将数组分成至少 k+1
个元素的组。这样每个元素的合法位置要么在元素当前所在的组内,要么在组内的左侧或右侧,但不能更远。然后对每个组进行排序。
这一步需要 O((n/k) * k log k) = O(n log k)
。
然后,在对每个组进行排序后,您可以将第 i
组与 i+1
组合并,对于 i
来自 1
到 n/(k+1) - 1
。
通过合并,我了解合并排序的合并过程。团体不团结。它们的大小保持不变。
每次合并需要 O(n/k)
,总共这一步是 O(n)
。
关于algorithm - 对具有 O(n*Log(K)) 复杂度的近排序数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55615707/
好吧,假设我有一堆光盘放在已知固定位置的飞机上。每个圆盘的半径为 1 个单位。该平面完全被一组圆盘覆盖,事实上,它被一组圆盘广泛覆盖,在某些区域覆盖了一两个数量级。我想找到仍然完全覆盖飞机的光盘子集。
我有一个涉及大量相关表的系统。考虑一个标准的类别/产品/订单/客户/订单项目场景。有些表是自引用的(如类别)。这些表都不是特别大(大约 10 万行,估计规模约为 100 万行)。我需要考虑这些数据的很
我正在学习 https://near.academy/near101/chapter-6 中的教程 其中一个步骤是运行此命令(但使用我的帐户): near call museum.testnet ad
我正在启动一个分析项目,该项目将处理数百万地理定位数据。数据可能是这样的: 编号{ 用户身份, 长, 纬度, 时间, 应用ID } 我的主要操作: 获取区域中包含的所有数据 找到属于某个userId的
在性能方面,JSON 解析需要大量时间来检索数据。在我的应用程序中,我需要从服务器获取近 10,000 条记录。在模拟器上,它立即获取数据并高效工作。但在我的 android 手机中,它需要超过2 分
任何人都可以帮助我从投影矩阵 44 获得左、右、下、上、近和远边界值吗? 最佳答案 这里是方程组的分辨率 Christian Rau引用: 对于正交矩阵: near = (1+m34)/m33;
我正在通过后台线程将 1,00,000 条记录插入到数据库中。此时,当我想要加载 Ui 屏幕时,出现内存不足错误。例如,当堆大小为 5 MB 且分配给后台线程的内存为 4 MB 时,加载 UI 屏幕需
C++如何存储近100000位的海量数字?.. 我试过使用 long long int 和 long double int..对我没有任何作用.. 有没有其他方法可以存储这么大的数字? 我希望找到大于
我是一名优秀的程序员,十分优秀!