-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenomicRangeQuery.js
More file actions
41 lines (33 loc) · 809 Bytes
/
GenomicRangeQuery.js
File metadata and controls
41 lines (33 loc) · 809 Bytes
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
// https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query/
function solution(S, P, Q) {
const hash = {
A: 1,
C: 2,
G: 3,
T: 4,
}
const nums = []
let beforeChar
S.split('').forEach((c, i) => {
if (beforeChar === c) {
nums[nums.length - 1].end++
} else if (['A', 'C', 'G', 'T'].includes(c)) {
nums.push({
val: hash[c],
start: i,
end: i
})
beforeChar = c
}
})
const result = []
for(let i = 0; i < P.length; i++) {
const start = P[i]
const end = Q[i]
const arr = nums
.filter(num => (num.start >= start && num.start <= end) || (num.end >= start && num.end <= end))
.sort((a,b) => a.val - b.val)
result.push(arr[0].val)
}
return result
}