我最近遇到了一个面试问题。
We have
m*n
matrix such that each row is in non-decreasing order (sorted with distinct elements). design an algorithm on orderO(m (log m+ log n))
to findk
-th smallest element on this matrix (just one element ask
-th smallest element).
我认为这是不可能的,所以在谷歌上搜索并找到 this link和 another solution和 this answer to a similar question .
我认为如下:
O(m)
中找到该数组的中位数并称之为枢轴O(m log n)
中找到该元素的等级.即:在每一行中有多少元素低于在步骤 (1) 中找到的枢轴。k
和“枢轴的等级”我们可以知道在每一行中都适用于右半部或左半部。 (减少到 m*n/2
矩阵。)但是这个算法的时间复杂度是
O(m * log^2 n)
.什么是可以工作的算法O(m (log n + log m))
?有什么想法吗?
最佳答案
m - 行
n - 列
O(m (log m+ log n))
的解决方案?复杂?O(k * log-m)
额外空间为 O(m)class PQObject {
int value; // PQ sorting happens on this int..
int m; // And m and n are positions.
int n;
}
关于java - 具有 O(m (log n + log m)) 时间复杂度的算法,用于在每行排序的 n*m 矩阵中查找第 k 个最小元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65748657/