Skip to content

Instantly share code, notes, and snippets.

@atonamy
Created May 18, 2019 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atonamy/5e438f0f7cd04c88a1fd828ca5c06dfa to your computer and use it in GitHub Desktop.
Save atonamy/5e438f0f7cd04c88a1fd828ca5c06dfa to your computer and use it in GitHub Desktop.
100% perfomance/correctness solution for GenomicRangeQuery problem https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query
val String.prefixSumOfGenoms: Array<IntArray>
get() {
val genoms = Array(3) {
IntArray(length+1)
}
for(i in 0 until length) {
var (a, c, g) = arrayOf<Short>(0, 0, 0)
when(this[i]) {
'A' -> {a = 1}
'C' -> {c = 1}
'G' -> {g = 1}
}
genoms[0][i+1] = genoms[0][i] + a
genoms[1][i+1] = genoms[1][i] + c
genoms[2][i+1] = genoms[2][i] + g
}
return genoms
}
fun Array<IntArray>.getMinImpact(startIndex: Int, endIndex: Int): Int {
var impact = 4
run loop@{
forEachIndexed { index, genoms ->
if (genoms[endIndex] - genoms[startIndex] > 0) {
impact = index + 1
return@loop
}
}
}
return impact
}
fun solution(S: String, P: IntArray, Q: IntArray): IntArray{
val genoms = S.prefixSumOfGenoms
val result = IntArray(P.size)
for(i in 0 until P.size)
result[i] = genoms.getMinImpact(P[i], Q[i]+1)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment