-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenomicRangeQuery.js
More file actions
49 lines (42 loc) · 1.46 KB
/
GenomicRangeQuery.js
File metadata and controls
49 lines (42 loc) · 1.46 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
//Score: 100%
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S, P, Q) {
// write your code in JavaScript (Node.js 8.9.4)
var leftDistanceToA = new Array(S.length).fill(+Infinity);
var leftDistanceToC = new Array(S.length).fill(+Infinity);
var leftDistanceToG = new Array(S.length).fill(+Infinity);
var leftDistanceToT = new Array(S.length).fill(+Infinity);
for (var s = 0; s < S.length; s++){
if(s!=0){
leftDistanceToA[s] = leftDistanceToA[s-1]+1;
leftDistanceToC[s] = leftDistanceToC[s-1]+1;
leftDistanceToG[s] = leftDistanceToG[s-1]+1;
leftDistanceToT[s] = leftDistanceToT[s-1]+1;
}
var c = S[s];
if(c+""=="A")
leftDistanceToA[s] = 0;
if(c+""=="C")
leftDistanceToC[s] = 0;
if(c+""=="G")
leftDistanceToG[s] = 0;
if(c+""=="T")
leftDistanceToT[s] = 0;
}
var ret = new Array(P.length).fill(0);
for (var k = 0; k < ret.length; k++){
var p = P[k];
var q = Q[k];
var maxLeft = q - p;
if(leftDistanceToA[q] <= maxLeft)
ret[k] = 1;
else if(leftDistanceToC[q] <= maxLeft)
ret[k] = 2;
else if(leftDistanceToG[q] <= maxLeft)
ret[k] = 3;
else if(leftDistanceToT[q] <= maxLeft)
ret[k] = 4;
}
return ret;
}