简体   繁体   English

具有 O(m (log n + log m)) 时间复杂度的算法,用于在 n*m 矩阵中查找第 k 个最小元素,每行排序?

[英]Algorithm with O(m (log n + log m)) time complexity for finding kth smallest element in n*m matrix with each row sorted?

I ran into an interview question recently.我最近遇到了一个面试问题。

We have m*n matrix such that each row is in non-decreasing order (sorted with distinct elements).我们有m*n矩阵,使得每一行都按非递减顺序(用不同的元素排序)。 design an algorithm on order O(m (log m+ log n)) to find k -th smallest element on this matrix (just one element as k -th smallest element).设计一个O(m (log m+ log n))阶的算法来找到这个矩阵上的第k个最小元素(只有一个元素作为第k个最小元素)。

I think this is not possible so search on Google and find this link and another solution and this answer to a similar question .我认为这是不可能的,所以在谷歌上搜索并找到这个链接另一个解决方案以及这个类似问题的答案

I think as follows:我认为如下:

  1. Put the median of all rows into an array and we find the median of this array in O(m) and called it pivot将所有行的中值放入一个数组中,我们在O(m)中找到该数组的中值,并将其命名为 pivot

  2. We find the rank of this element in O(m log n) .我们在O(m log n)中找到该元素的等级。 ie: in each row how many elements are lower than the pivot found in step (1).即:在每一行中有多少元素低于步骤(1)中找到的 pivot。

  3. By comparing k and "rank of the pivot" we can know that in each row works on the right half or left half.通过比较k和“枢轴的等级”,我们可以知道每一行都在右半边或左半边工作。 (reduce to m*n/2 matrix.) (减少到m*n/2矩阵。)

But the time complexity of this algorithm is O(m * log^2 n) .但是这个算法的时间复杂度是O(m * log^2 n) What is the algorithm that can works on O(m (log n + log m)) ?可以在O(m (log n + log m))上运行的算法是什么? Is there any idea?有什么想法吗?

m - rows n - columns m - 行 n - 列

  • Is it compulsory that you want a solution with O(m (log m+ log n)) Complexity?您是否需要具有O(m (log m+ log n))复杂度的解决方案?

  • I Can Think of a Solution with Complexity O(k * log-m) with extra space of O(m)我可以想出一个复杂度为O(k * log-m)且额外空间为 O(m) 的解决方案

    • You can use a modified PriorityQueue (heap) DataStructure for this complexity对于这种复杂性,您可以使用修改后的 PriorityQueue(堆)DataStructure
     class PQObject { int value; // PQ sorting happens on this int.. int m; // And m and n are positions. int n; }
  • You can just put all the values from the first column to the Priority Queue and Start popping until the kth smallest element您可以将第一列中的所有值放入优先级队列并开始弹出直到第 k 个最小元素

    • Every time you pop re-insert the next value of the row using m and n in the popped object.每次弹出时,在弹出的 object 中使用 m 和 n 重新插入行的下一个值。
  • Ultimately the problem comes down to find the kth smallest element in M sorted arrays.最终问题归结为在 M 排序的 arrays 中找到第 k 个最小的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此方法的时间复杂度为2 * O(n log n)+ O(m log m)? - Why is this method's time complexity 2*O(n log n) + O(m log m)? O(n log n)时间复杂度算法? - O(n log n) Time Complexity Algorithm? 这个算法不会在O(m log n)中运行吗? - Wouldn't this algorithm run in O(m log n)? 斐波那契分析-这种解决方案的时间复杂度为log(n)或((m(n)* log n))吗? - Fibonacci analysis - Is this solution in log(n) or ((m(n) *log n)) time complexity? 如何确定时间复杂度是O(m + n)还是O(Math.max(m,n)) - How to determine if the time complexity is O(m + n) or O(Math.max(m, n)) 优化程序以查找数组中第k个最小元素(Java,预期时间复杂度O(n)) - Optimization of Program to find kth smallest element in an array (Java, Expected time complexity O(n)) 关于时间复杂度 O(1), O(n), O(log n), O(n log n) 的问题 - Questions about Time complexity O(1), O(n), O(log n), O(n log n) 为什么这个算法的运行时间是 O(m+n)? - Why is the runtime of this algorithm O(m+n)? HashMap 具有 O(log(N)) 时间复杂度操作 - HashMap with O(log(N)) time complexity operations 以O(log n)复杂度计算排序数组中值的出现次数 - Count the number of appearances of a value in a sorted array in O(log n) complexity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM