Created
July 7, 2013 17:44
-
-
Save kaja47/5944287 to your computer and use it in GitHub Desktop.
They said I can't have fast TopK, but I proved them wrong.
And they stabbed me in the stomach.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def topKEstaimateMin(arr: Array[Double], k: Int): Double = { | |
import java.lang.Integer | |
val upperK = Integer.highestOneBit(k) * 2 // must be higher than k, otherwise can produce wrong minimum | |
val bits = Integer.numberOfTrailingZeros(upperK) | |
val mask = (1 << bits) - 1 | |
var i = 0 | |
val maximums = new Array[Double](upperK) | |
while (i < arr.length) { | |
val pos = i & mask | |
val m = maximums(pos) | |
val a = arr(i) | |
maximums(pos) = (if (m < a) a else m) | |
i += 1 | |
} | |
var min = Double.PositiveInfinity | |
var j = 0 | |
while (j < upperK) { | |
min = (if (maximums(j) < min) maximums(j) else min) | |
j += 1 | |
} | |
return min | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kukam na to uz pekne dlho a fakt nechapem. Mozes hodit nejaky odkaz na to aku fintu pouzivas? Lebo tento zapis algoritmu nedokazem rozlustit.