給定一個包含N個點的數組,在2D平面中查找K最接近 原點(0,0)的點。你可以假設K比N小得多,N非常大。此代碼的時間複雜度
E.g:
given array: (1,0), (3,0), (2,0), K = 2 Result = (1,0), (2,0)
(結果應該是在由距離升序)
代碼:
import java.util.*;
class CPoint {
double x;
double y;
public CPoint(double x, double y) {
this.x = x;
this.y = y;
}
}
public class KClosest {
/**
* @param myList: a list of myList
* @param k: the number of closest myList
* @return: the k closest myList
*/
public static CPoint[] getKNearestPoints(CPoint[] myList, int k) {
if (k <= 0 || k > myList.length) return new CPoint[]{};
if (myList == null || myList.length == 0) return myList;
final CPoint o = new CPoint(0, 0); // origin point
// use a Max-Heap of size k for maintaining K closest points
PriorityQueue<CPoint> pq = new PriorityQueue<CPoint> (k, new Comparator<CPoint>() {
@Override
public int compare(CPoint a, CPoint b) {
return Double.compare(distance(b, o), distance(a, o));
}
});
for (CPoint p : myList) { // Line 33
// Keep adding the distance value until heap is full. // Line 34
pq.offer(p); // Line 35
// If it is full // Line 36
if (pq.size() > k) { // Line 37
// Then remove the first element having the largest distance in PQ.// Line 38
pq.poll(); // Line 39
} // Line 40
}
CPoint[] res = new CPoint[k];
// Then make a second pass to get k closest points into result.
while (!pq.isEmpty()) { // Line 44
res[--k] = pq.poll(); // Line 45
} // Line 46
return res;
}
private static double distance(CPoint a, CPoint b) {
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
}
問題:
第35行第39行的獨立時間複雜度和 分別是多少?
第35-40行(整體)的時間複雜度是多少?
第44-46行(整體)的時間複雜度是多少?
整個方法getKNearestPoints()的整體時間複雜度是多少,在最好,最差和平均情況下?如果n >> k? 以及如果我們沒有n >> k呢?
其實這些問題都是我的技術面試時的幾個問題,但我仍然在它有點困惑。任何幫助表示讚賞。
這看起來很像家庭作業。你到底迷惑了什麼?你認爲答案是什麼?爲什麼? – Krease
我在回答採訪時回答:Q1:log(K),log(K),Q2:log(K)或log(K)^ 2 Q3:klog(K)Q4:NlogK + klogK,但總體來說是NlogK?我不知道 – Peter
我知道PQ,所有的操作像添加/提供,刪除/輪詢需要OlogK,除非偷看是O1。但專門針對這些問題。我真的有點失落.. – Peter