我會在這個代碼,但我竭力要了解它是如何爲O(M + N )而不是O(Math.max(m,n))。或者是O(Math.max(m,n))下的O(m + n)呢?如何確定的時間複雜度爲O(M + N)或O(Math.max(M,N))
int i = 0, j = 0, res = 0;
while (i < houses.length) {
while (j < heaters.length - 1
&& Math.abs(heaters[j + 1] - houses[i]) <= Math.abs(heaters[j] - houses[i])) {
j++;
}
res = Math.max(res, Math.abs(heaters[j] - houses[i]));
i++;
}
有一個關於CTCI的例子,其中函數返回一個n大小的數組。它說,由於計算N> LOGN大O字時,導致O(n)的整體因調用堆棧的log(n)的空間複雜度是小巫見大巫。在這個例子中沒有提到O(n + logn)(對於那些好奇的人來說是4.4)。
任何解釋,將不勝感激!
在簡單的話,'M + N <= Math.Max(M,N)+ Math.Max(M,N)= O(Math.Max(M,N))';因此結果。 –