-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenomicRangeQuery.kt
More file actions
78 lines (58 loc) · 1.91 KB
/
GenomicRangeQuery.kt
File metadata and controls
78 lines (58 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package sideImplementations
import kotlin.math.floor
class GenomicRangeQuery {
companion object {
fun solution(s: String, p: IntArray, q: IntArray): IntArray {
val prefixSum = IntArray(s.length + 1)
var smallest = 5
if(s.length == 1) {
p[0] = convertNucleotideTypeToImpactFactor(s[0])
return p
}
// calculate the prefix sum
for(k in 1 until prefixSum.size) {
val factor = convertNucleotideTypeToImpactFactor(s[k-1])
if (factor < smallest) {
smallest = factor
}
prefixSum[k] = prefixSum[k - 1] + factor
}
for(k in q.indices) {
if (p[k] == 0 && q[k] == 0) {
p[k] = convertNucleotideTypeToImpactFactor(s[k])
} else {
val floor = floor(sumInSlice(prefixSum, p[k], q[k]) / (q[k] - p[k] + (1/smallest)).toDouble()).toInt()
p[k] = if (floor == 0) {
1
} else {
floor
}
}
}
return p
}
private fun convertNucleotideTypeToImpactFactor(type: Char): Int {
return when (type) {
'A' -> {
1
}
'C' -> {
2
}
'G' -> {
3
}
else -> {
4
}
}
}
private fun sumInSlice(prefixSum: IntArray, firstPositionInSlice: Int, lastPositionInSlice: Int): Int {
var first = firstPositionInSlice
if(first == 0) {
first++
}
return prefixSum[lastPositionInSlice + 1] - prefixSum[first]
}
}
}